Subscriber callback inside class not recognized C++
I have created a class that contains a ROS subscriber and the callback function. I have done this following the ROS tutorial and according to other answers that I have found (for example here).
I have included a snippet of my code below:
class VelocityCommandSubscriber{
public:
geometry_msgs::Twist velocity_msg;
std::string velocity_type;
VelocityCommandSubscriber(geometry_msgs::Twist velocity_msg, std::string velocity_type)
{
velocity_msg = velocity_msg;
velocity_type = velocity_type;
velocitySubscriber = nH.subscribe<std_msgs::Float64>("/command_velocity_"+velocity_type,10, &VelocityCommandSubscriber::VelocityCallback, this);
}
void VelocityCallback(std_msgs::Float64::ConstPtr& velocity_cmd_msg)
{
//Callback code here
}
protected:
ros::NodeHandle nH;
ros::Subscriber velocitySubscriber;
};
However, whenever I run this code I get the following error
error: no matching function for call to ‘ros::NodeHandle::subscribe<std_msgs::Float64>(std::__cxx11::basic_string<char>, int, void (VelocityCommandSubscriber::*)(std_msgs::Float64_<std::allocator<void> >::ConstPtr&), VelocityCommandSubscriber*)’
81 | velocitySubscriber = nH.subscribe<std_msgs::Float64>("/command_velocity_"+velocity_type,10, &VelocityCommandSubscriber::VelocityCallback, this); |
Does anyone know what am I doing wrong? I am using ROS noetic and am relatively new to ROS and c++ so I may very well be reading over something simple.
Thanks for your help!
Your callback argument looks different from the example from the ROS Answers, can you try it with normal
const
instead ofConstrPtr
? Or use the suggestedboost::bind
?I tried it with
const
and I get the exact same error. I also tried boost::bind, but that just gave me another similar error. Is this maybe new in noetic since the other answers would have been for an older ROS version?@ljaniec , I think you may have been correct. As I said in a reply to the answer I think defining the type in the subscriber template was causing mismatch errors. Thanks for your help!