【ROS】使用ros::Timer週期性呼叫函式遇到bug
阿新 • • 發佈:2020-08-10
背景
在類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() {}即可。