-1

in my base class, this is a public non-static member function:

void BaseClass::SetPorcessMethod()
{
     //do something
    Listener.support(methods::POST, this->HandlePost);
    //do something
}

In the function above, Listener is a static member variable. Function HandlePost is a pure virtual function which is implemented by all derived classes. I want to use this pointer to call different HandlePost from different derived class. Like:

class BaseClass
{
 public:
   static http_listener listener;
   void SetPorcessMethod();
   virtual void HandlePost((http_request request) = 0;
   /*listener is init in constructor , not display here*/
}
 class Derived2:public BaseClass
{
  void HandlePost(http_request request);
}
class Derived1:public BaseClass
{
  void HandlePost(http_request request);
}

Derived1 instance1;
instance1.SetPorcessMethod();
Derived2 instance2;
instance2.SetPorcessMethod();

However, it displaysBaseClass::HandlePost:function call missing argument list use &BaseClass::HandlePost:function to create a pointer to member.I know this is because Your code attempts to pass a pointer-to-member-function, which cannot be converted to a pointer-to-function. This is because a pointer-to-member-function can only be called on an object, so it wouldn't know what object to use. Function call missing argument list to create pointer But what should I do so that I can call the function from derived class withsupport()?

Community
  • 1
  • 1
firstaccount
  • 155
  • 2
  • 13
  • Something essential is missing from this question because it makes no sense at all. – Edward Strange Oct 04 '16 at 23:02
  • @CrazyEddie which information do you need ? – firstaccount Oct 04 '16 at 23:06
  • 1
    What part of "use &BaseClass::HandlePost" you didn't understand? Seems like a fairly clear error message, and it is correct: this is the right syntax for specifying a pointer to a class method. Note that this is a pointer to a class method, and not a function, so the second parameter to your `support()` method has better be a `void (BaseClass::*)(http_request)`. If it isn't, you have bigger problems, such as broken class design. – Sam Varshavchik Oct 04 '16 at 23:13
  • @SamVarshavchik Update the question – firstaccount Oct 04 '16 at 23:28
  • @Eichhörnchen I want to pass the function to support – firstaccount Oct 05 '16 at 00:03

1 Answers1

2

You are trying to pass a member function to support. In order to call such a function the caller need the member function arguments and the pointer to the instance to call the function on.

However support expects a std::function<void(http_request)>, i.e. without the instance pointer. So you have to wrap the call into another callable which does not need to have the BaseClass instance pointer passed. You can do that with a lambda (or std::bind if you prefer):

Listener.support( methods::POST,
    [this](http_request request){return HandlePost(request);} );

Listener.support( methods::POST,
    std::bind(&BaseClass::HandlePost, this, std::placeholders::_1) );

#include<functional> for the latter variant.

  • Where did you get the idea that `support()` expects a `std::function` as input? That is not stated anywhere in the question, edits, or comments. What is stated is that the compiler error is expecting a `pointer-to-function`, so `support()` is actually expecting a plain `void (*)(http_request)` function pointer instead. And you can't use a capturing lambda for that. – Remy Lebeau Oct 05 '16 at 02:44
  • 1
    @RemyLebeau The question is not clear on it, but considering the tags, I assume that they are using this library: https://github.com/Microsoft/cpprestsdk. I looked up the signature of `Listener.support` assuming `Listener` is a `http_listener` from that library. Also that quote in the question is not part of the error message, but quoted from another question, I think. –  Oct 05 '16 at 03:05
  • @firstaccount Does the second one not work? It could be that I made an mistake, if so I'd like to fix it. –  Oct 05 '16 at 17:39
  • It seems that "&" is illegal when I use the second way. – firstaccount Oct 05 '16 at 21:56
  • @firstaccount Ah yes, I forgot that this syntax is not allowed, one has to be a bit more verbose. I fixed it. –  Oct 05 '16 at 22:06