a error occurs when using boost::bind to the basic listener.cpp [closed]

asked 2017-05-25 04:30:41 -0600

Xiang gravatar image

Hi,

I am using boost bind to subscribe to several topics, following this.

I programmed a basic listener.cpp according to the wiki, and added boost::bind to it. Unfornately, the errors occured.

Can anyone help me? Many thanks.

#include "ros/ros.h"
#include "std_msgs/String.h"
#include <boost/bind.hpp>

void foo(int a, int b) {
    std::cout << "a + b = " << (a + b) << std::endl;

    return;
}

void chatterCallback(const std_msgs::String::ConstPtr& msg) {
    ROS_INFO("I heard: [%s]", msg->data.c_str());
}

int main(int argc, char **argv) {
    //foo(1, 2);
    // it works good for foo
    //boost::bind(foo, _1, 5)(1);

    ros::init(argc, argv, "listener_bind");

    ros::NodeHandle n;
    //ros::Subscriber sub = n.subscribe("chatter0", 1000, chatterCallback);
    ros::Subscriber sub = n.subscribe("chatter0", 1000, boost::bind(chatterCallback, _1));

    ros::spin();

    return 0;
}

The resulting log shows

    /home/alex/Documents/Tutorials/ROS/wiki/catkin_ws/src/beginner_tutorials/src/listener_bind.cpp: In function ‘int main(int, char**)’:
/home/alex/Documents/Tutorials/ROS/wiki/catkin_ws/src/beginner_tutorials/src/listener_bind.cpp:23:89: error: no matching function for call to ‘ros::NodeHandle::subscribe(const char [9], int, boost::_bi::bind_t<void, void (*)(const boost::shared_ptr<const std_msgs::String_<std::allocator<void> > >&), boost::_bi::list1<boost::arg<1> > >)’
     ros::Subscriber sub = n.subscribe("chatter0", 1000, boost::bind(chatterCallback, _1));
                                                                                         ^
/home/alex/Documents/Tutorials/ROS/wiki/catkin_ws/src/beginner_tutorials/src/listener_bind.cpp:23:89: note: candidates are:
In file included from /opt/ros/indigo/include/ros/ros.h:45:0,
                 from /home/alex/Documents/Tutorials/ROS/wiki/catkin_ws/src/beginner_tutorials/src/listener_bind.cpp:1:
/opt/ros/indigo/include/ros/node_handle.h:401:14: note: template<class M, class T> ros::Subscriber ros::NodeHandle::subscribe(const string&, uint32_t, void (T::*)(M), T*, const ros::TransportHints&)
   Subscriber subscribe(const std::string& topic, uint32_t queue_size, void(T::*fp)(M), T* obj, 
              ^
/opt/ros/indigo/include/ros/node_handle.h:401:14: note:   template argument deduction/substitution failed:
/home/alex/Documents/Tutorials/ROS/wiki/catkin_ws/src/beginner_tutorials/src/listener_bind.cpp:23:89: note:   mismatched types ‘void (T::*)(M)’ and ‘boost::_bi::bind_t<void, void (*)(const boost::shared_ptr<const std_msgs::String_<std::allocator<void> > >&), boost::_bi::list1<boost::arg<1> > >’
     ros::Subscriber sub = n.subscribe("chatter0", 1000, boost::bind(chatterCallback, _1));
                                                                                         ^
In file included from /opt/ros/indigo/include/ros/ros.h:45:0,
                 from /home/alex/Documents/Tutorials/ROS/wiki/catkin_ws/src/beginner_tutorials/src/listener_bind.cpp:1:
/opt/ros/indigo/include/ros/node_handle.h:412:14: note: template<class M, class T> ros::Subscriber ros::NodeHandle::subscribe(const string&, uint32_t, void (T::*)(M)const, T*, const ros::TransportHints&)
   Subscriber subscribe(const std::string& topic, uint32_t queue_size, void(T::*fp)(M) const, T* obj, 
              ^
/opt/ros/indigo/include/ros/node_handle.h:412:14: note:   template argument deduction/substitution failed:
/home/alex/Documents/Tutorials/ROS/wiki/catkin_ws/src/beginner_tutorials/src/listener_bind.cpp:23:89: note:   mismatched types ‘void (T::*)(M)const’ and ‘boost::_bi::bind_t<void, void (*)(const boost::shared_ptr<const std_msgs::String_<std::allocator<void> > >&), boost::_bi::list1<boost::arg<1> > >’
     ros::Subscriber sub ...
(more)
edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by Xiang
close date 2017-05-25 06:10:19.006768

Comments

Never mind, my mistake about understanding of boost::bind(). I closed the question. Sorry.

Xiang gravatar image Xiang  ( 2017-05-25 06:09:40 -0600 )edit