使用 boost::bind 的“呼叫不匹配”错误

Isa*_*acS 5 c++ boost-bind

我还是 boost::bind 的新手,现在正在移植一个 2 年前在 2009 年编写的程序,看到下面的编译错误。任何解决方法的想法将不胜感激。

提取的cpp文件:

class ClassA {
private:
    cNamespace::Bounds bounds_msg_;

    void boundsHandler(const PublisherPtr& p) { 
        p->publish(bounds_msg_);
    }

    void funcA() {
        node_->advertise<cNamespace::Bounds>("bounds", 10,
              boost::bind(&ClassA::boundsHandler, this, _1));  // <---- Line 445
    }
};
Run Code Online (Sandbox Code Playgroud)

CMake 时出错:

/home/userA/ClassA.cpp:445:   instantiated from here
/usr/include/boost/bind/bind.hpp:313: error: no match for call to ‘(boost::_mfi::mf1<void, ClassA, const PublisherPtr&>) (ClassA*&, const ros::SingleSubscriberPublisher&)’
Run Code Online (Sandbox Code Playgroud)

环境:Ubuntu 10.10,g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5

可能没有必要,但功能API的参考advertise在这里,或者:

template<class M >
Publisher   advertise (const std::string &topic, 
                       uint32_t queue_size, 
                       const SubscriberStatusCallback &connect_cb, 
                       const SubscriberStatusCallback &disconnect_cb=SubscriberStatusCallback(),
                       const VoidConstPtr &tracked_object=VoidConstPtr(), 
                       bool latch=false)
Run Code Online (Sandbox Code Playgroud)

Arv*_*vid 5

看起来生成的函数对象boost::bind是用与您绑定的函数不同的类型调用的。

即它是用const ros::SingleSubscriberPublisher&参数调用的,而不是预期的const PublisherPtr& p

假设SubscriberStatusCallback是 a boost::function,您应该确保其参数与您绑定的参数匹配。