0

I have a class whose member function I am trying to point to, problem is I keep getting this error reference to non-static member function must be called which from my understanding is that a member function needs to be pointed to. The problem is, when I try to use this solution, the compiler complains because there is no viable conversion from 'void (Foo::*) (const List&) to std::function<void (const List &)>

This is my 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;
}
Community
  • 1
  • 1
i_use_the_internet
  • 604
  • 1
  • 9
  • 28

1 Answers1

4

you can do like this:

void Foo::HandleRequest(HTTPServerRequest* request){
    std::function<void (const List&)> functor =
        std::bind(&Foo::callback_method, this, std::placeholders::_1);
}

or:

void Foo::HandleRequest(HTTPServerRequest* request){
    std::function<void (const List&)> functor = 
        [this](const List& list){callback_method(list);};
}
A.S.H
  • 29,101
  • 5
  • 23
  • 50
于明亮
  • 62
  • 3