在 class 中将函数作为参数传递时出错

Getting error while passing a function as a parameter within a class

我正在尝试使用 ESPAsyncWebServer 库做一些事情。我在 ServerManager.h 中有一个名为 MyServerManager 的 class。当我编译代码时出现此错误。我究竟做错了什么?我该如何解决?

25:67 是粗体字“this

src\ServerManager.cpp: In lambda function: src\ServerManager.cpp:25:67: error: 'this' was not captured for this lambda function request->send(LittleFS, "/status.html", String(), false, [this](const String &var) -> String { return statusProcessor(var); });

*** [.pio\build\nodemcuv2\src\ServerManager.cpp.o] 错误 1

在 ServerManager.cpp 文件中

void MyServerManager::init()
{
    if (!LittleFS.begin())
    {
        Serial.println("Something went wrong while begin LittleFS");
    }

    server->on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
        request->send(LittleFS, "/status.html", String(), false, [this](const String &var) -> String { return statusProcessor(var); });
    });
    server->begin();
}

String MyServerManager::statusProcessor(const String &var)
{
    if (var == "NETWORK_NAME")
    {
        return WiFi.SSID();
    }
    else if (var == "SIGNAL_STRENGTH")
    {
        return String(WiFi.RSSI()) + " dBm";
    }

    return String();
}

在 ServerManager.h 文件中

#ifndef ServerManager_h
#define ServerManager_h
#include <Arduino.h>
#include <ESPAsyncWebServer.h>

class MyServerManager
{
private:
    AsyncWebServer *server = NULL;
    AsyncWebSocket *ws = NULL;
    void notifyClients(const String *msg);
    void handleWebSocketMessage(void *arg, uint8_t *data, size_t len);
    void onEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len);
    void initWebSocket();
    String statusProcessor(const String &var);

public:
    MyServerManager(uint16_t port, const String url);
    ~MyServerManager();

    void init();
};

#endif

尝试将 this 添加到外部 lambda 捕获列表:

void MyServerManager::init()
{
    if (!LittleFS.begin())
    {
        Serial.println("Something went wrong while begin LittleFS");
    }
    //                       > vvvv <
    server->on("/", HTTP_GET, [this](AsyncWebServerRequest *request) {
        request->send(LittleFS, "/status.html", String(), false, [this](const String &var) -> String { return statusProcessor(var); });
    });
    server->begin();
}