我正在尝试将某些类方法传递给某些函数并出现“函数调用缺少参数列表;使用‘&’创建指向成员的指针”错误。
//There is some class
class A {
int someField;
void Add(int someAdd) {
someField += someAdd;
}
}
//And function
void someFunc(std::function<void(int x)> handler) {
//Some code
handler(234);
}
//Class method pass to function
void main() {
A* instanceA = new A();
someFunc(instanceA->Add); //Error 19 error C3867: 'A::Add': function call missing argument list; use '&A::Add' to create a pointer to member
}
怎么了?
最佳答案
C++ 没有作为语言构造的绑定(bind)方法。写:
someFunc(std::bind(&A::Add, instanceA, std::placeholders::_1));
关于c++ - 类方法作为函数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14684580/