c++arduino-c++

How to pass a variable to a function in c++


I am trying to pass a String variable to a function but its throwing an error

no matching function for call to 'AsyncWebServer::on(String&, WebRequestMethod, processor(const String&)::<lambda(AsyncWebServerRequest*)>)'

The reason is because I`m trying to pass variable r to a function, but when I pass to a function some string in quotes"" it works just fine.

NOT works :

String r = randString();
server.on(r , HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(SPIFFS, "/index.html", String(), false);
});

works :

server.on("/jjj", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(SPIFFS, "/index.html", String(), false, processor);
});

Solution

  • Here is the solution:

    String r = randString();
    r = "/" + r;
    const char* b = r.c_str();
    
    server.on(b , HTTP_GET, [](AsyncWebServerRequest *request){
        request->send(SPIFFS, "/index.html", String(), false);
    });