Binding parameter to callback using boost::bind does not compile

asked 2017-07-09 07:59:49 -0500

Paddre gravatar image

updated 2017-07-10 07:58:33 -0500

The following Code compiles just fine:

#include "MyMsgType.h"

class MyNode
{
  MyNode()
  {
    sub_ = nh_.subscribe<MyMsgType>("networking/transmit_token", 1000, &MyNode::callback, this);
  }

  template <class T>
  void callback(const boost::shared_ptr<T const> &message)
  {
    ROS_DEBUG("Callback");
  }

  ros::Subscriber sub_;
  ros::NodeHandle nh_;
}

If I want to add a parameter to method callback, I modifiy the code accordingly:

#include "MyMsgType.h"

class MyNode
{
  MyNode()
  {
    sub_ = nh_.subscribe<MyMsgType>("networking/transmit_token", 1000, boost::bind(&MyNode::callback, this, _1, 0));
  }

  template <class T>
  void callback(const boost::shared_ptr<T const> &message, int n)
  {
    ROS_DEBUG("Callback");
  }

  ros::Subscriber sub_;
  ros::NodeHandle nh_;
}

This however won't compile. Error:

error: no matching function for call to ‘bind(<unresolved overloaded function type>, MyNode*, const boost::arg<1>&, int)’

     sub_ = nh_.subscribe<MyMsgType>("networking/transmit_token", 1000, boost::bind(&MyNode::callback, this, _1, 0));

[...]

note: candidate: template<class R, class F, class A1, class A2, class A3> boost::_bi::bind_t<R, F, typename boost::_bi::list_av_3<A1, A2, A3>::type> boost::bind(F, A1, A2, A3)

     BOOST_BIND(F f, A1 a1, A2 a2, A3 a3)
     ^

note:   template argument deduction/substitution failed:

note:   could not deduce template parameter ‘R’

     subs_.push_back(nh_.subscribe<cooperative_driving_networking::Token>("networking/transmit_token", 1000, boost::bind(&WiFiMonitor::callback, this, _1, 0)));

Boost version: 1.64

What's wrong here? :-/

edit retag flag offensive close merge delete