ros中NodeHandle类的subscribe()函数使用报错问题

在使用ros写订阅者时,代码如下:

#include "ros/ros.h"
#include "std_msgs/String.h"
#include <boost/thread.hpp>
#include <sensor_msgs/PointCloud2.h>
#include <nav_msgs/Odometry.h>
#include <typeinfo>

class multiThreadListener
{
public:
    void chatterCallback1(const nav_msgs::Odometry::ConstPtr &input_msg, std::string topicName);
    void chatterCallback2(const sensor_msgs::PointCloud2ConstPtr& input_msg, std::string topicName);
    multiThreadListener();

private:
    ros::NodeHandle n;
    std::string name1;
    std::string name2;
    ros::Subscriber sub1;
    ros::Subscriber sub2;
};

multiThreadListener::multiThreadListener()
{
    name1 = "/navsat/odom";
    name2 = "/radar_cloud";

    sub1 = n.subscribe("/navsat/odom", 10, boost::bind(&multiThreadListener::chatterCallback1, this, _1, name1));
    sub2 = n.subscribe("/radar_cloud", 10, boost::bind(&multiThreadListener::chatterCallback2, this,_1, name2));
}

void multiThreadListener::chatterCallback1(const nav_msgs::Odometry::ConstPtr &input_msg, std::string topicName)
{
  ROS_INFO("I heard: [%s]", topicName);
  ros::Rate loop_rate(0.5);//block chatterCallback1()
  loop_rate.sleep();
}

void multiThreadListener::chatterCallback2(const sensor_msgs::PointCloud2ConstPtr& input_msg, std::string topicName)
{
  ROS_INFO("I heard: [%s]", topicName);
  ros::Rate loop_rate(0.5);//block chatterCallback2()
  loop_rate.sleep();
}

int main(int argc, char **argv)
{

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

  multiThreadListener listener_obj;
  ros::MultiThreadedSpinner s(2);
  ros::spin(s);

  return 0;
}

编译总是报如下错,

error: no matching function for call to ‘ros::NodeHandle::subscribe(const char [13], int, boost::_bi::bind_t<void, boost::_mfi::mf2<void, multiThreadListener, const boost::shared_ptr<const nav_msgs::Odometry_<std::allocator<void> > >&, std::__cxx11::basic_string<char> >, boost::_bi::list3<boost::_bi::value<multiThreadListener*>, boost::arg<1>, boost::_bi::value<std::__cxx11::basic_string<char> > > >)’  sub1 = n.subscribe("/navsat/odom", 10, boost::bind(&multiThreadListener::chatterCallback1, this, _1, name1));

原因是subscribe()函数在使用函数对象时,如boost::bind()函数返回的对象,必须要明确指定消息类型作为模板参数,因为编译器无法推断它。

上面就是没有指明导致的错误。

在查看NodeHandle.h源码查看subscribe()函数的定义如下

template<class M, class T>
Subscriber subscribe(const std::string& topic, uint32_t queue_size, void(T::*fp)(M), T* obj, const TransportHints& transport_hints = TransportHints())
{
     ...
}

template<class M, class T>
Subscriber subscribe(const std::string& topic, uint32_t queue_size, void(T::*fp)(const boost::shared_ptr<M const>&), T* obj, const TransportHints& transport_hints = TransportHints())
{
    ...
}

template<class M, class T>
Subscriber subscribe(const std::string& topic, uint32_t queue_size, void(T::*fp)(M), const boost::shared_ptr<T>& obj, const TransportHints& transport_hints = TransportHints())
{
    ...
}

template<class M, class T>
Subscriber subscribe(const std::string& topic, uint32_t queue_size, void(T::*fp)(const boost::shared_ptr<M const>&), const boost::shared_ptr<T>& obj, const TransportHints& transport_hints = TransportHints())
{
    ...
}

template<class M>
Subscriber subscribe(const std::string& topic, uint32_t queue_size, void(*fp)(M), const TransportHints& transport_hints = TransportHints())
{
    ...
}

template<class M>
Subscriber subscribe(const std::string& topic, uint32_t queue_size, void(*fp)(const boost::shared_ptr<M const>&), const TransportHints& transport_hints = TransportHints())
{
    ...
}

template<class M>
Subscriber subscribe(const std::string& topic, uint32_t queue_size, const boost::function<void (const boost::shared_ptr<M const>&)>& callback,
                           const VoidConstPtr& tracked_object = VoidConstPtr(), const TransportHints& transport_hints = TransportHints())
{
    ...
}

template<class M, class C>
Subscriber subscribe(const std::string& topic, uint32_t queue_size, const boost::function<void (C)>& callback,
                           const VoidConstPtr& tracked_object = VoidConstPtr(), const TransportHints& transport_hints = TransportHints())
{
    ...
}

很显然,subscribe是模板函数,在传入boost::function类型时,需要传入模板参数,上面的代码

sub2 = n.subscribe("/radar_cloud", 10, boost::bind(&multiThreadListener::chatterCallback2, this,_1, name2));

传入了三个参数,第三个参数中的chatterCallback2()的第一个参数时sensor_msgs::PointCloud2ConstPtr,其定义是

typedef boost::shared_ptr< ::sensor_msgs::PointCloud2 const> sensor_msgs::PointCloud2ConstPtr

显然,上面代码使用的subscribe()函数是下面这个版本

template<class M>
Subscriber subscribe(const std::string& topic, uint32_t queue_size, const boost::function<void (const boost::shared_ptr<M const>&)>& callback,
                           const VoidConstPtr& tracked_object = VoidConstPtr(), const TransportHints& transport_hints = TransportHints())
{
    ...
}

故,正确的代码应该如下

#include "ros/ros.h"
#include "std_msgs/String.h"
#include <boost/thread.hpp>
#include <sensor_msgs/PointCloud2.h>
#include <nav_msgs/Odometry.h>
#include <typeinfo>

class multiThreadListener
{
public:
    void chatterCallback1(const nav_msgs::Odometry::ConstPtr &input_msg, std::string topicName);
    void chatterCallback2(const sensor_msgs::PointCloud2ConstPtr& input_msg, std::string topicName);
    multiThreadListener();

private:
    ros::NodeHandle n;
    std::string name1;
    std::string name2;
    ros::Subscriber sub1;
    ros::Subscriber sub2;
};

multiThreadListener::multiThreadListener()
{
    name1 = "/navsat/odom";
    name2 = "/radar_cloud";

    sub1 = n.subscribe<nav_msgs::Odometry>("/navsat/odom", 10, boost::bind(&multiThreadListener::chatterCallback1, this, _1, name1));
    sub2 = n.subscribe<sensor_msgs::PointCloud2>("/radar_cloud", 10, boost::bind(&multiThreadListener::chatterCallback2, this,_1, name2));
}

void multiThreadListener::chatterCallback1(const nav_msgs::Odometry::ConstPtr &input_msg, std::string topicName)
{
  ROS_INFO("I heard: [%s]", topicName);
  ros::Rate loop_rate(0.5);//block chatterCallback1()
  loop_rate.sleep();
}

void multiThreadListener::chatterCallback2(const sensor_msgs::PointCloud2ConstPtr& input_msg, std::string topicName)
{
  ROS_INFO("I heard: [%s]", topicName);
  ros::Rate loop_rate(0.5);//block chatterCallback2()
  loop_rate.sleep();
}

int main(int argc, char **argv)
{

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

  multiThreadListener listener_obj;
  ros::MultiThreadedSpinner s(2);
  ros::spin(s);

  return 0;
}

编译通过。

原文地址:https://www.cnblogs.com/kerngeeksund/p/11136909.html

时间: 2024-10-29 20:57:50

ros中NodeHandle类的subscribe()函数使用报错问题的相关文章

Android Fragment 嵌套使用报错

在新的SDK每次创建activity时,会自动生成 <pre name="code" class="java">public static class PlaceholderFragment extends Fragment fragment模块,在该模块的基础上进行嵌套fragment代码如下: <pre name="code" class="java">public static class Pla

UEditor使用报错Cannot set property &#39;innerHTML&#39; of undefined

仿用UEditor的setContent的时候报错,报错代码如下Uncaught TypeError: Cannot set property 'innerHTML' of undefined.调试ueditor.config.js,ueditor.all.js 提示me.body is undefined.(我就纳了闷儿了!同样的使用那个行这个咋就不行,那个有body,view,这个咋就这么少![我还查看了插件的内部函数,真是个good媛~])错误的原因是没有等UEditor创建完成就使用UE

Windows下Git使用报错:warning:LF will be replaced by CRLF in &#215;&#215;&#215;&#215;.&#215;&#215;

Windows下Git使用报错: warning:LF will be replaced by CRLF in ××××.××(文件名) The file will have its original line ending in your working directory. 翻译: 在xxx.xx文件中LF将被CRLF替换. 在工作区(working directory)里,这个文件将会保持它原本的换行符.(line ending:行尾,换行) 注解:           LF:Line F

使用报错:Fatal error in launcher: Unable to create process using &#39;&quot;&#39;

在一个系统中共存Python2.python3的时候,pip.pip2.pip3使用的时候会报错: c:\Python35\Scripts>pip3Fatal error in launcher: Unable to create process using '"' 解决方法: python3: python3 -m pip install --upgrade pip python2: python2 -m pip install --upgrade pip 注意:python2, pyt

56-python pip使用报错:Fatal error in launcher: Unable to create process using &#39;&quot;&#39;

在一个系统中共存Python2.python3的时候,pip.pip2.pip3使用的时候会报错: c:\Python35\Scripts>pip3Fatal error in launcher: Unable to create process using '"' 解决方法: python3: python3 -m pip install --upgrade pip python2: python2 -m pip install --upgrade pip 注意:python2, pyt

Alamofire使用报错Extra argument &#39;method&#39; in call解决办法

使用Alamofire的时候,在用这句的时候报错了: Extra argument 'method' in call Alamofire.request("", method: HTTPMethod.get, parameters: [], encoding: JSONEncoding.default, headers: nil).responseJSON { (response) in //是否请求成功 if let jsonValue = response.result.value

[Cordova+Sencha Touch] 移动开发1 sencha 2.4.0 + 在 安卓2.3.6上使用报错 - has no method &#39;bind&#39;

Sencha Touch 2.3.2和2.4.0在安卓2.3上面用会报错,具体报错信息如下: 解决办法是: 打开文件:你的file:///android_asset/www/sencha-touch-all-debug.js 找到第76728行: if (Ext.feature.has.Touch) { // bind handlers that are only invoked when the browser has touchevents me.onTargetTouchMove = me

python pip使用报错:Fatal error in launcher: Unable to create process using &#39;&quot;&#39;

系统win7 解决方法: python3: python3 -m pip install --upgrade pip python2: python2 -m pip install --upgrade pip 注意:python2, python3共存时,将python安装目录下的python.exe改成对应的python2.exe,python3.exe python pip使用报错:Fatal error in launcher: Unable to create process using

ipmitool使用报错Could not open device at /dev/ipmi0 or /dev/ipmi/0 or /dev/ipmidev/0: No such file or directory

ipmitool使用报错处理 Could not open device at /dev/ipmi0 or /dev/ipmi/0 or /dev/ipmidev/0: No such file or directory 解决办法:需要加载相关模块 查看先关模块是否加载(可以看出模块未加载) #lsmod |grep ^ipmi 加载以下模块 # modprobe ipmi_watchdog # modprobe ipmi_poweroff # modprobe ipmi_devintf # m