ROS 开发人员提供了一个简洁的替代 subscribe 方法,它接受成员函数和相应的对象,如下所示:
sub = n.subscribe("/camera/depth_registered/points", 1000, &Example::callBack, this);
&Example::callBack
是指向成员函数的函数指针,这是你要为其调用回调的对象实例。
问题待解决:
error: no matching function for call to ‘ros::NodeHandle::subscribe<geometry_msgs::Twist>(const char [9], int, void (dog_driver::DogDriverNode::*)(geometry_msgs::Twist_<std::allocator<void> >::ConstPtr), dog_driver::DogDriverNode*)’
sub_vel = n_.subscribe<geometry_msgs::Twist>("/cmd_vel", 5, &DogDriverNode::cmdVelHandler, this);
暂时不能确定为什么会出现以上报错。
不过幸运的是,即使第一种方法不能使用,还有另一种方法。第二种选择是使用 boost::bind()
,这是一个非常强大的工具,可以将参数绑定到任意函数,它也支持类成员函数:
sub = n.subscribe("/camera/depth_registered/points", 1000, boost::bind(&Example::callBack, this, _1));
语法稍微复杂一些,但它更通用。