即使使用此功能,boost也不会绑定到成员函数

时间:2010-05-11 14:55:12

标签: c++ boost boost-bind

我正在尝试将boost::bindboost::function一起使用。 这似乎是一个微不足道的例子,但我无法使其发挥作用。你能救我吗?

是因为不允许或我做错了吗?

// .h
class MyClass{
publc:
    void DoSomething( 
        const std::string& a,
        const std::string& b);
    void DoABind();

}

//.cpp
void MyClass::DoABind(){

    boost::function< void( const std::string& , const std::string& ) > callback( 
        boost::bind(
               &MyClass::DoSomething,
                 this ));

        // this line doesn't compile!!!
}

2 个答案:

答案 0 :(得分:3)

我想你想要bind(&MyClass::DoSomething, this, _1, _2)。我没有使用boost进行测试。

答案 1 :(得分:3)

您忘记使用参数占位符。试试这个:

boost::function< void( const std::string& , const std::string& ) > callback(
    boost::bind(
           &MyClass::DoSomething,
             this, _1, _2 ));

这在gcc 4.4.1上编译,增强1.41。