简体   繁体   中英

c++ <unresolved overloaded function type>

I am new to c++. I am trying to convert an existing and functioning c style code to c++. It is the ESP8266WebServer for arduino core. It uses ESP8266WebServer which is a c++ class. I am trying to create a class that includes the above class as an object. However the following code generates the error listed below. I know I can just use the example code that came with the IDE but then I won't be learning much. Any help greatly appreciated.

    #include <ESP8266WiFi.h>
    #include <WiFiClient.h>
    #include <ESP8266WebServer.h>
    #include <ESP8266mDNS.h>

    class myServer {
        public:
          const char* ssid="mySSID";
          const char* pass="myPASS";
          myServer(int n);
          void start();
          void handlers();
          void sayHello();
        private:
          ESP8266WebServer serv;
      };

    myServer::myServer(int n){ 
      ESP8266WebServer serv(n);  
    }

    void myServer::start(){
      WiFi.begin(ssid, pass);  
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
    }

    void myServer::sayHello() {serv.send(200, "text/plain", "hello from esp8266!");}

    void myServer::handlers(){
        if (MDNS.begin("esp8266")) {Serial.println("MDNS responder started");}
        serv.on("/", sayHello);
        //on("/inline", [](){send(200, "text/plain", "this works as well");});
        //onNotFound();
        serv.begin();
    }

    //----------------------

    myServer server(80);

    void setup(){
      server.start();
      server.handlers();
    }

    void loop() {
      //server.handleClient(); //goes in to loop
    }

I get the following errors.

    /home/mrpong/Arduino/myserver/myserver.ino: In member function 'void myServer::handlers()':
    myserver:34: error: no matching function for call to 'ESP8266WebServer::on(const char [2], <unresolved overloaded function type>)'
         serv.on("/", sayHello);
                              ^
    /home/mrpong/Arduino/myserver/myserver.ino:34:26: note: candidates are:
    In file included from /home/mrpong/Arduino/myserver/myserver.ino:3:0:
    /home/mrpong/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/libraries/ESP8266WebServer/src/ESP8266WebServer.h:79:8: note: void ESP8266WebServer::on(const char*, ESP8266WebServer::THandlerFunction)
       void on(const char* uri, THandlerFunction handler);
            ^
    /home/mrpong/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/libraries/ESP8266WebServer/src/ESP8266WebServer.h:79:8: note:   no known conversion for argument 2 from '<unresolved overloaded function type>' to 'ESP8266WebServer::THandlerFunction {aka std::function<void()>}'
    /home/mrpong/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/libraries/ESP8266WebServer/src/ESP8266WebServer.h:80:8: note: void ESP8266WebServer::on(const char*, HTTPMethod, ESP8266WebServer::THandlerFunction)
       void on(const char* uri, HTTPMethod method, THandlerFunction fn);
            ^
    /home/mrpong/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/libraries/ESP8266WebServer/src/ESP8266WebServer.h:80:8: note:   candidate expects 3 arguments, 2 provided
    /home/mrpong/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/libraries/ESP8266WebServer/src/ESP8266WebServer.h:81:8: note: void ESP8266WebServer::on(const char*, HTTPMethod, ESP8266WebServer::THandlerFunction, ESP8266WebServer::THandlerFunction)
       void on(const char* uri, HTTPMethod method, THandlerFunction fn, THandlerFunction ufn);
            ^
    /home/mrpong/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/libraries/ESP8266WebServer/src/ESP8266WebServer.h:81:8: note:   candidate expects 4 arguments, 2 provided
    exit status 1
    no matching function for call to 'ESP8266WebServer::on(const char [2], <unresolved overloaded function type>)'

Any help greatly appreciated. Thanks

void f();
struct A { void m(); };

f is a function, but there is no m , instead there is A::m which is a member function that requires an instance of A to be invoked against. From inside of A we can refer to it as just m , but don't be fooled.

The ESP8266Webserver::on is declared thus:

typedef std::function<void(void)> THandlerFunction;
void on(const String &uri, THandlerFunction handler);

it requires a function . You are trying to pass it a member function . That's not possible.

In older code you'll see an approach where you pass a function pointer and an argument, and then the trampoline is a static function that combines the two:

static void trampoline(myServer* ptr)
{
     ptr->sayHello();
}

void myServer::handlers(){
     //...
     on("/", trampoline, this);
}

After your problem line in your code there's an example of a lambda:

//on("/inline", [](){send(200, "text/plain", "this works as well");});

The on definition uses std::function because this allows the use of capturing lambdas as an argument like this, letting us do pretty much whatever we want as long as it doesn't take any arguments or return anything.

From inside a member-function of my struct A above, writing m(); looks to the compiler like this->m(); . Using a lambda we use this to create what's called a trampoline:

void myServer::handlers(){
    if (MDNS.begin("esp8266")) {Serial.println("MDNS responder started");}
    serv.on("/", [this](){ sayHello(); });
    serv.on("/inline", [](){send(200, "text/plain", "this works as well");});
    //onNotFound();
    serv.begin();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM