-1

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?

Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119
  • 1
    You want the lambda to do its thing and provide you with the `AsyncWebServerRequest*`? After defining, you can execute it with `()`. So `[]() {}()`. But the lambda you've written returns nothing according to the error message. How this compiles at all for the Arduino is quite confusing. – sweenish Dec 07 '21 at 23:00
  • @Michel Keijzers What is unclear with the error message? – Vlad from Moscow Dec 07 '21 at 23:10
  • 2
    Because as declared in your bottom code, `on()` takes a pointer to an `AsyncWebServerRequest` **object** that you called function. That's not the syntax of a function pointer. If you want a function pointer, you're going to have to explain what the return type is and what parameters it's supposed to take. It looks like it should be `void (*function)(AsyncWebServerRequest*)`, based on the lambda you wrote, but spelling out the requirements would be better. – sweenish Dec 07 '21 at 23:11
  • @sweenish; I indeed made a mistake (also in the prototype). – Michel Keijzers Dec 08 '21 at 09:40
  • I don't think the answer is right. What did you change Michel? – Juraj Dec 08 '21 at 20:38
  • @Juraj I changed the last argument type and the return type (was void). Btw, it doesn't work yet on the ESP32 but that is high likely as I made so many changes something (else?) is (still) wrong, but it compiles correct on both PC and ESP32. – Michel Keijzers Dec 09 '21 at 08:39

1 Answers1

2

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.

user8510613
  • 1,242
  • 9
  • 27
  • I don't understand. The answer is true but it doesn't explain the problem. ArRequestHandlerFunction is typedef for a function with one parameter of type AsyncWebServerRequest*. so it is the same and the examples of the library use the same way the code in question does. the problem is something else. – Juraj Dec 08 '21 at 20:37
  • @Juraj the origin type is ```std::function``` and the mock signature accept ```AsyncWebServerRequest*```, how could it be the same? – user8510613 Dec 09 '21 at 01:51
  • the examples of the library have `[](AsyncWebServerRequest* request)` and compile without errors. here the difference is that Michel has the lambda in a class `RelayBoxServer::Send:: – Juraj Dec 09 '21 at 04:35