问题: ESP8266 AsyncWebServer 仅在我将其实例化为全局时才有效,但在我将其实例化为类时无效。
这是两个都产生错误的完整 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);
^
分析 这让我感到困惑,因为错误消息似乎说调用签名是错误的。然而,它在类之外的效果是一样的。疯了吗?
在 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){}
};
关于arduino - 如何在类中实例化 ESP AsyncWebServer? (arduino esp8266),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62239472/