繁体   English   中英

std :: bind绑定到非静态成员函数会引发很多错误

[英]std::bind to non-static member function throwing a bunch of errors

我有一个Button类,并且正在尝试向其添加一个回调函数。 在回答这个问题之后,这是我的课程的布局:

class Button {
public:
    void SetCallbackFunc(std::function<void()> const& f) {
        callbackFunc = f;
    }
private:
    std::function<void()> callbackFunc;
};

class SomeOtherClass {
public:
    void DoThings();
    void SetupButtonFunctionality() {
        Button *b = new Button();
        b->SetCallbackFunc(std::bind(&SomeOtherClass::DoThings, this, std::placeholders::_1));
    }
};

这些都是我得到的所有编译错误:

error C2977: 'std::add_reference' : too many template arguments
error C2955: 'std::add_reference' : use of class template requires template argument list 
error C2146: syntax error : missing ',' before identifier 'type' (Content\SomeOtherClass.cpp)
error C2065: 'type' : undeclared identifier (Content\SomeOtherClass.cpp)
error C2064: term does not evaluate to a function taking 2 arguments (Content\SomeOtherClass.cpp)
error C2064: term does not evaluate to a function taking 0 arguments (GUI\Button.cpp)
error C2027: use of undefined type 'std::tuple_element<0,_Ftuple>'

代码有什么问题? 还有另一种简单的方式可以写我想要的东西吗?

该函数不带参数,因此您不需要占位符参数。

b->SetCallbackFunc(std::bind(&SomeOtherClass::DoThings, this));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM