1

我正在尝试boost::bind使用boost::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!!!
}
4

2 回答 2

3

我想你想要bind(&MyClass::DoSomething, this, _1, _2)。不过,我没有要测试的 boost 安装。

于 2010-05-11T15:06:02.143 回答
3

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

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

这在带有 boost 1.41 的 gcc 4.4.1 上编译。

于 2010-05-11T15:06:12.640 回答