背景

在类A内,声明了一个ros::timer变量,然后使用该定时器周期性调用函数。遇到了报错。

类A代码如下,

A.h 类的声明

class A
{        void initA(ros::NodeHandle& nh);        ros::NodeHandle node_;        ros::Timer xxxx_timer_;
    
        bool radarMapUpdate(const ros::TimerEvent& /*event*/);
};

A.cpp 类的定义

void A::initA(ros::NodeHandle& nh)
{
    node_ = nh;

    xxxx_timer_ = node_.createTimer(ros::Duration(0.05), &A::radarMapUpdate, this);
}

bool A::radarMapUpdate()
{
    ....;
}

报错内容如下,

/home/gordon/ros_ws/src/xxxxxxxx/radar_grid_map.cc:1402:93:   required from here
/usr/include/boost/function/function_template.hpp:231:11: error: no match for call to ‘(boost::_mfi::mf1<bool, xag_nav::planning::RadarGridMap, const ros::TimerEvent&>) (const ros::TimerEvent&)’
           BOOST_FUNCTION_RETURN(boost::mem_fn(*f)(BOOST_FUNCTION_ARGS));
           ^
In file included from /usr/include/boost/bind/mem_fn.hpp:215:0,
                 from /usr/include/boost/mem_fn.hpp:22,
                 from /usr/include/boost/function/detail/prologue.hpp:18,
                 from /usr/include/boost/function.hpp:24,
                 from /opt/ros/kinetic/include/ros/forwards.h:40,
                 from /opt/ros/kinetic/include/ros/common.h:37,
                 from /opt/ros/kinetic/include/ros/ros.h:43,
                 from /home/gordon/ros_ws/src/xxxxxx/radar_grid_map.h:13,
                 from /home/gordon/ros_ws/src/xxxxxx/radar_grid_map.cc:3:
/usr/include/boost/bind/mem_fn_template.hpp:163:7: note: candidate: R boost::_mfi::mf1<R, T, A1>::operator()(T*, A1) const [with R = bool; T = xag_nav::planning::RadarGridMap; A1 = const ros::TimerEvent&]
     R operator()(T * p, A1 a1) const
       ^
/usr/include/boost/bind/mem_fn_template.hpp:163:7: note:   candidate expects 2 arguments, 1 provided
/usr/include/boost/bind/mem_fn_template.hpp:168:25: note: candidate: template<class U> R boost::_mfi::mf1<R, T, A1>::operator()(U&, A1) const [with U = U; R = bool; T = xag_nav::planning::RadarGridMap; A1 = const ros::TimerEvent&]
     template<class U> R operator()(U & u, A1 a1) const
                         ^
/usr/include/boost/bind/mem_fn_template.hpp:168:25: note:   template argument deduction/substitution failed:
In file included from /usr/include/boost/function/detail/maybe_include.hpp:18:0,
                 from /usr/include/boost/function/detail/function_iterate.hpp:14,
                 from /usr/include/boost/preprocessor/iteration/detail/iter/forward1.hpp:52,
                 from /usr/include/boost/function.hpp:64,
                 from /opt/ros/kinetic/include/ros/forwards.h:40,
                 from /opt/ros/kinetic/include/ros/common.h:37,
                 from /opt/ros/kinetic/include/ros/ros.h:43,
                 from /home/gordon/ros_ws/src/xxxxxxx/radar_grid_map.h:13,
                 from /home/gordon/ros_ws/src/xxxxxxx/radar_grid_map.cc:3:
/usr/include/boost/function/function_template.hpp:231:11: note:   candidate expects 2 arguments, 1 provided
           BOOST_FUNCTION_RETURN(boost::mem_fn(*f)(BOOST_FUNCTION_ARGS));
           ^
In file included from /usr/include/boost/bind/mem_fn.hpp:215:0,
                 from /usr/include/boost/mem_fn.hpp:22,
                 from /usr/include/boost/function/detail/prologue.hpp:18,
                 from /usr/include/boost/function.hpp:24,
                 from /opt/ros/kinetic/include/ros/forwards.h:40,
                 from /opt/ros/kinetic/include/ros/common.h:37,
                 from /opt/ros/kinetic/include/ros/ros.h:43,
                 from /home/gordon/ros_ws/src/xxxxxxx/radar_grid_map.h:13,
                 from /home/gordon/ros_ws/src/xxxxxxx/radar_grid_map.cc:3:
/usr/include/boost/bind/mem_fn_template.hpp:176:25: note: candidate: template<class U> R boost::_mfi::mf1<R, T, A1>::operator()(const U&, A1) const [with U = U; R = bool; T = xag_nav::planning::RadarGridMap; A1 = const ros::TimerEvent&]
     template<class U> R operator()(U const & u, A1 a1) const

解决方案

报错的意思,大致是找到了boost库中的候选函数,但是实参和形参对应不上!

据此,把函数 bool radarMapUpdate() {} 改成 void radarMapUpdate() {} 即可。

 

写在最后

由于对ros timer的理解不深,使用过程中会存在一些玄学的错误。比如,timer没有成功地周期性调用函数!

长个心眼。