I am programming for the ESP32 (a sort of Arduino like chip). However, to easier/faster find compiler errors/warnings and later make a sort of virtualization on the PC, I like to compile the code also on a PC (using Visual Studio).
However, I cannot get the following code to compile on a PC (while it compiles in the Arduino IDE for ESP32):
server.on("/", HTTP_GET, [](AsyncWebServerRequest* request)
{
request->send_P(200, "text/html", textBuffer, Processor);
});
The errors I get are:
Severity Code Description Project File Line Suppression State
Error (active) E0413 no suitable conversion function from "lambda []void (AsyncWebServerRequest *request)->void" to "AsyncWebServerRequest *" exists RelayBox C:\Users\miche\source\repos\RelayBox2\RelayBox\RelayBox\RelayBoxServer.cpp 203
Error C2664 'void AsyncWebServer::on(const char *,int,AsyncWebServerRequest *)': cannot convert argument 3 from 'RelayBoxServer::Send::<lambda_6ffcff35888ca3a1f1d7d541e1edeba3>' to 'AsyncWebServerRequest *' RelayBox C:\Users\miche\source\repos\RelayBox2\RelayBox\RelayBox\RelayBoxServer.cpp 206
The code is based on the ESP32 library at https://github.com/me-no-dev/ESPAsyncWebServer/blob/master/src/ESPAsyncWebServer.h
And the 'stub' classes I created using library above but simplified for my project, with '...' as irrelevant code:
class AsyncWebServer
{
...
void on(const char* something, int httpGet, AsyncWebServerRequest* function);
...
}
#define HTTP_GET 100
class AsyncWebServerRequest
{
public:
void send_P(int port, const char* textHmlt, STRING text, ProcessorHandlerFunction function);
};
class ArduinoStringStub : public std::string
{
...
}
How can I fix the argument type conversion compiler errors in this method using a lambda function?
CodePudding user response:
I've checked your reference github, it seems that the origin AsyncWebServer
class has such signature:
AsyncCallbackWebHandler& on(const char* uri, WebRequestMethodComposite method, ArRequestHandlerFunction onRequest);
And ArRequestHandlerFunction
is actually:
typedef std::function<void(AsyncWebServerRequest *request)> ArRequestHandlerFunction;
So it seems to be that the stub/mock AsyncWebServer
you create should not take AsyncWebServerRequest*
but ArRequestHandlerFunction
as the on
method's last parameter, corresponding with the original code.