如何在类中实例化 ESP AsyncWebServer?(Arduino esp8266)

Ale*_*ton 3 webserver global arduino esp8266

问题: ESP8266 AsyncWebServer 仅当我将其实例化为全局时才有效,但当我在类中实例化它时则无效。

这是两个完整的草图,它们都会产生错误

方法 1:在没有构造函数的情况下尝试此方法来初始化:

#include <ESPAsyncWebServer.h>

//  AsyncWebServer server(80);  // works when I instantiate it here

class foo {
  AsyncWebServer server(80);  // fails if instead I instantiate it here
  int dummy{0};
};

void setup() {}
void loop() {}

Run Code Online (Sandbox Code Playgroud)

错误信息1

sketch_jun06d:6:25: error: expected identifier before numeric constant
   AsyncWebServer server(80);  // this does not work
                         ^
sketch_jun06d:6:25: error: expected ',' or '...' before numeric constant
Run Code Online (Sandbox Code Playgroud)

方法2 在构造函数中初始化

class foo{
  public:

 AsyncWebServer server;
 foo() {
   server(80);
 };
};
Run Code Online (Sandbox Code Playgroud)

错误信息2

In constructor 'foo::foo()':
sketch_jun06d:18:8: error: no matching function for call to 'AsyncWebServer::AsyncWebServer()'
  foo() {
        ^
Arduino/sketch_jun06d/sketch_jun06d.ino:18:8: note: candidates are:
In file included from sketch_jun06d.ino:1:0:
libraries/ESPAsyncWebServer-master/src/ESPAsyncWebServer.h:405:5: note: AsyncWebServer::AsyncWebServer(uint16_t)
     AsyncWebServer(uint16_t port);
     ^
libraries/ESPAsyncWebServer-master/src/ESPAsyncWebServer.h:405:5: note:   candidate expects 1 argument, 0 provided
libraries/ESPAsyncWebServer-master/src/ESPAsyncWebServer.h:397:7: note: AsyncWebServer::AsyncWebServer(const AsyncWebServer&)
 class AsyncWebServer {
       ^
libraries/ESPAsyncWebServer-master/src/ESPAsyncWebServer.h:397:7: note:   candidate expects 1 argument, 0 provided
sketch_jun06d:19:13: error: no match for call to '(AsyncWebServer) (int)'
    server(80);
             ^
Run Code Online (Sandbox Code Playgroud)

分析 这让我感到困惑,因为错误消息似乎表明调用签名是错误的。然而,它在课堂外的运作方式是一样的。疯狂的?

在 Mac 上使用 Arduino.app 1.8.12 的平台和目标。

Arduino: 1.8.12 (Mac OS X), Board: "WeMos D1 R1, 80 MHz, Flash, Legacy (new can return nullptr), All SSL ciphers (most compatible), 4MB (FS:2MB OTA:~1019KB), v2 Lower Memory, Disabled, None, Only Sketch, 3000000"
Run Code Online (Sandbox Code Playgroud)

版本: 我在 ESPAsyncWebServer 文件中找不到明确的版本号,但我看到未解压的代码在文件上具有 2019 年 10 月的日期。

Del*_*a_G 7

在构造函数上使用初始值设定项列表。

这是未编译和未经测试的,但至少应该让您朝着正确的方向前进。

class foo {
  AsyncWebServer server;  // fails if instead I instantiate it here
  int dummy;

  foo() : server(80), dummy(0){}
};
Run Code Online (Sandbox Code Playgroud)