c++ - ROS 订阅回调 - 使用 boost::bind 和成员函数

标签 c++ c++11 boost ros reference-wrapper

我正在尝试使用相同的回调来订阅 ROS 中的不同主题(每辆弹出的车辆对应一个主题)。这个想法是boost::bind将主题名称作为附加参数传递,以便我知道我应该在回调中访问哪个工具。

问题是,尽管我已经回答了关于该主题的多个问题,但似乎没有一个解决方案有效。

基本上,我有以下类(class) VOBase包含 std::map<std::string, VOAgent*> agents_成员函数如下:

void VOBase::callback_agentState(const custom_msgs::VState::ConstPtr& vStateMsg,
        std::string topic) {
    // [...] regex to find agent name from topic string
    std::string agent_name = match.str();
    // [...] Check if agent name exists, else throw exception

    // Process message
    agents_[agent_name]->pos_ = vStateMsg->pose.position;  // etc.
}

我正在通过此订阅调用:

void VOBase::callback_agentList(const custom_msgs::VehicleList& vehListMsg) {
    // [...] New agent/vehicle found: process vehListMsg and get agent_name string

    // Subscribe to VState
    topic_name = agent_name + "/state_estimate";
    subscribers_[topic_name] = nodeHandlePtr->subscribe<custom_msgs::VState>(topic_name, 1,
        std::bind(&VOBase::callback_agentState, this, _1, topic_name));
}

但是,我得到一个 template argument deduction/substitution failed所有候选人和这个错误:

mismatched types ‘std::reference_wrapper<_Tp>’ and ‘VO::VOBase*’
                    typename add_cv<_Functor>::type&>::type>()(

我已经测试了很多解决方案,例如使用 std::ref(this)得到 std::reference_wrapper<_Tp>而不是 VO::VOBase* (尽管引用文献不存在: use of deleted function ),使用 boost::bind而不是 std::bind (但从 C++11 开始它应该完全相同),有和没有 ...::ConstPtr对于回调函数参数(以及 subscribe<acl_msgs::ViconState::ConstPtr> 等)中的 ROS 消息,所以我只是在处理部分解决方案及其排列……

有什么线索吗?

最佳答案

我没有查看您显示的代码的细节(很难找出未显示的信息并推断出需要什么)。

但是,上次我帮助某人进行 ROS 订阅和类型推导时,很明显处理程序应该对消息采用 shared_ptr。这可能会帮助您开始寻找解决方案:

关于c++ - ROS 订阅回调 - 使用 boost::bind 和成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45899300/

相关文章:

c++ - 从函数返回 mpz_t

c++ - 自定义复制动态数组

c++ - 使用类和命名空间差异/歧义

c++ - 执行时 : Issue with never ending loop

c++ - 使用 strtok_s 分割字符串

c++ - 将我的代码打包为库时应该创建 .a 还是 .so?

c++ - boost::intrusive::unordered_set 桶中使用的是什么存储策略?

c++ - 在 MinGW 中使用 Boost 时出现问题

c++ - C++传递参数时 "small object"的定义

python - boost python hello程序上的导入错误