當我在終端中編譯一個ROS的C++程序,出現以下錯誤:
In file included from /opt/ros/indigo/include/ros/subscription_callback_helper.h:35:0,
from /opt/ros/indigo/include/ros/subscriber.h:33,from /opt/ros/indigo/include/ros/node_handle.h:33,
from /opt/ros/indigo/include/ros/ros.h:45,
from /home/qingdu/catkin_ws/src/youbot_match/src/youbot_match.cpp:5:
/opt/ros/indigo/include/ros/parameter_adapter.h: In instantiation of ‘static ros::ParameterAdapter<M>::Parameter ros::ParameterAdapter<M>::getParameter(const Event&) [with M = std_msgs::Int32MultiArray_<std::allocator<void> >&; ros::ParameterAdapter<M>::Parameter = std_msgs::Int32MultiArray_<std::allocator<void> >&; ros::ParameterAdapter<M>::Event = ros::MessageEvent<const std_msgs::Int32MultiArray_<std::allocator<void> > >; typename boost::remove_reference<typename boost::remove_const<M>::type>::type = std_msgs::Int32MultiArray_<std::allocator<void> >]’:
/opt/ros/indigo/include/ros/subscription_callback_helper.h:144:54: required from ‘void ros::SubscriptionCallbackHelperT<P, Enabled>::call(ros::SubscriptionCallbackHelperCallParams&) [with P = std_msgs::Int32MultiArray_<std::allocator<void> >&; Enabled = void]’
/home/qingdu/catkin_ws/src/youbot_match/src/youbot_match.cpp:327:1: required from here
/opt/ros/indigo/include/ros/parameter_adapter.h:78:30: error: invalid initialization of reference of type ‘ros::ParameterAdapter<std_msgs::Int32MultiArray_<std::allocator<void> >&>::Parameter {aka std_msgs::Int32MultiArray_<std::allocator<void> >&}’ from expression of type ‘const std_msgs::Int32MultiArray_<std::allocator<void> >’
return *event.getMessage();
^
make[2]: *** [youbot_match/CMakeFiles/youbot_match_node.dir/src/youbot_match.cpp.o] Error 1
make[1]: *** [youbot_match/CMakeFiles/youbot_match_node.dir/all] Error 2
make: *** [all] Error 2
後來經過定位才知道在定義話題訂閱subscribe函數中的回調函數時,參數沒有加上const 關鍵字 例子如下:
void firePositionCallBack(const std_msgs::Int32MultiArray& msg){
} //回調函數
int main(int argc, char **argv) {
ros::init(argc, argv, "youbot_move");
ros::NodeHandle n;
ros::Subscriber fire_position_sub = n.subscribe("fire_detection_all",10,firePositionCallBack);
return 0;
}