我有一个类,其成员函数我试图指出,问题是我不断收到此错误reference to non-static member function must be called
从我的理解是需要指向成员函数。问题是,当我尝试使用this解决方案时,编译器会抱怨因为there is no viable conversion from 'void (Foo::*) (const List&) to std::function<void (const List &)>
这是我的Foo
课程:
class Foo {
public:
int Run( int port);
void HandleRequest(HTTPServerRequest* request);
private:
int num_ports;
void callback_method(const List& );
}; //class Foo
void Foo::HandleRequest(HTTPServerRequest* request){
std::function<void (const List&)> functor = callback_method;
}
答案 0 :(得分:4)
void Foo::HandleRequest(HTTPServerRequest* request){
std::function<void (const List&)> functor =
std::bind(&Foo::callback_method, this, std::placeholders::_1);
}
或:
void Foo::HandleRequest(HTTPServerRequest* request){
std::function<void (const List&)> functor =
[this](const List& list){callback_method(list);};
}