【问题标题】:ROS: subscribe function does not recognize my callback methodROS:订阅函数无法识别我的回调方法
【发布时间】:2012-02-26 01:21:03
【问题描述】:

我收到此错误“错误:没有匹配的函数来调用‘ros::NodeHandle::subscribe(const char [24], int, <unresolved overloaded function type>)’

这是我在 BangBangControlUnit 类中的回调函数

// on message reciept: 'current_maintained_temp' void current_maintained_temp_callback(const std_msgs::Int32::ConstPtr& msg){ temp_to_maintain = msg->data;
}

这就是我在主函数中使用订阅的方式

// subscribe to 'current_maintained_temp' ros::Subscriber current_maintained_temp_sub = n.subscribe("current_maintained_temp", 1000, control.current_maintained_temp_callback);

谁能告诉我我做错了什么?

【问题讨论】:

    标签: c++ publish-subscribe robot ros


    【解决方案1】:

    使用类方法作为回调创建订阅者的正确签名如下:

    ros::Subscriber sub = nh.subscribe("my_topic", 1, &Foo::callback, &foo_object);
    

    所以在你的情况下你应该使用:

    current_maintained_temp_sub = n.subscribe("current_maintained_temp", 1000, &BangBangControlUnit::current_maintained_temp_callback, &control);
    

    您可以阅读有关 C++ 中发布者和订阅者的更多信息here

    【讨论】:

    • 只是一个小提示:您可以轻松地将订阅者包含在回调函数所属的类中,使事情更加连贯。只需将最后一个参数替换为 this 关键字即可告诉订阅者该对象是它所属的类。
    猜你喜欢
    • 1970-01-01
    • 2019-02-22
    • 1970-01-01
    • 2022-06-27
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 2018-07-22
    相关资源
    最近更新 更多