Functor -> 必须调用对非静态成员函数的引用
Functor -> reference to non-static member function must be called
我有一个 class 我试图指向其成员函数,问题是我不断收到此错误 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:
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;
}
你可以这样做:
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);};
}
我有一个 class 我试图指向其成员函数,问题是我不断收到此错误 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:
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;
}
你可以这样做:
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);};
}