如何在 class 中实例化 ESP AsyncWebServer? (arduino esp8266)

How to instantiate ESP AsyncWebServer inside a class? (arduino esp8266)

问题: ESP8266 AsyncWebServer 仅在我将其实例化为全局时才有效,但在 class.

中实例化时无效

这是两个都产生错误的完整 sketechs

方法 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() {}

错误消息 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

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

class foo{
  public:

 AsyncWebServer server;
 foo() {
   server(80);
 };
};

错误消息 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);
             ^

分析 这让我感到困惑,因为错误消息似乎说调用签名是错误的。但它在 class 之外的工作方式相同。疯了吗?

在 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"

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

在构造函数中使用初始化列表。

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

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

  foo() : server(80), dummy(0){}
};