How to use boost::bind() for callback member function in C++

asked 2021-03-23 23:18:07 -0500

CroCo gravatar image

I'm trying to wrap my node neatly inside a class. Callbacks are generally a problem to be a member data. There is a plethora of solutions. One of them is to use std::bind or boost::bind. I'm trying to use it for subscribe() but it yields an error.

 class NodeListener
      void chatterCallback(const std_msgs::String::ConstPtr& msg);
      ros::Subscriber sub;
void NodeListener::chatterCallback(const std_msgs::String::ConstPtr& msg)
   cout << this->i << endl;
void NodeListener::ros_init(...)
      sub = m_nh->subscribe("chatter",1000,boost::bind(&NodeListener::chatterCallback,this,_1));

This is the error I'm getting

error: no matching function for call to 'ros::NodeHandle::subscribe(const char[8],int,boost::_bi::bind_t<...>

Has subscribe() been modified so it longer accepts this syntax? Notice: this solution works just fine

sub = m_nh->subscribe("chatter",1000,&NodeListener::chatterCallback,this);
edit retag flag offensive close merge delete