c ++ <unresolved overloaded =“”function =“”type =“”>

时间:2016-10-01 04:47:11

标签: c++

我是c ++的新手。我正在尝试将现有的和正常运行的c样式代码转换为c ++。它是用于arduino核心的ESP8266WebServer。它使用ESP8266WebServer,这是一个c ++类。我正在尝试创建一个包含上述类作为对象的类。但是,以下代码生成下面列出的错误。我知道我可以使用IDE附带的示例代码,但后来我不会学到太多东西。非常感谢任何帮助。

    #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
    }

我收到以下错误。

    /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>)'

任何帮助非常感谢。 感谢

1 个答案:

答案 0 :(得分:5)

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

f是一个函数,但是没有m,而是A::m需要的成员函数要调用的A实例。从A内部我们可以将其称为m,但不要被愚弄。

因此宣布ESP8266Webserver::on

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

它需要功能。您正尝试将成员函数传递给它。那是不可能的。

在较旧的代码中,您将看到传递函数指针和参数的方法,然后trampoline是一个结合了两者的静态函数:

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

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

在您的代码中出现问题后,有一个lambda示例:

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

on定义使用std::function因为这允许使用捕获lambdas作为这样的参数,让我们做任何我们想做的事情,只要它不接受任何参数或返回任何东西。

从上面struct A的成员函数内部,编写m();看起来像编译器this->m();。使用lambda我们用它来创建所谓的蹦床:

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();
}