开发者

boost doesn't bind to member function even using this

开发者 https://www.devze.com 2022-12-30 09:39 出处:网络
I am trying to use boost::bind with a boost::function using this. It seems a trivial example but I cannot make it work. Can you help me?

I am trying to use boost::bind with a boost::function using this. It seems a trivial example but I cannot make it work. Can you help me?

Is it because it is not allowed or am I doing something wrong?

// .h
class MyClass{
publc:
    void DoSomething( 
        cons开发者_如何学Pythont 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!!!
}


I think you want bind(&MyClass::DoSomething, this, _1, _2). I don't have a boost installation to test with though.


You forgot to use the parameter placeholders. Try this:

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

This compiles on gcc 4.4.1 with boost 1.41.

0

精彩评论

暂无评论...
验证码 换一张
取 消