如何在一个类中实例化ESP AsyncWebServer? (arduino esp8266)

问题描述 投票:0回答:1

问题: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() {}

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

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

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() {
        ^
/Users/cems/Documents/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月的日期。

arduino webserver esp8266 globals
1个回答
1
投票

在构造函数上使用初始化程序列表。

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

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

  foo() : server(80), dummy(0){}
};
© www.soinside.com 2019 - 2024. All rights reserved.