英文:
Passing a function as a parameter within a class
问题
在处理传递 processor 函数到响应参数时遇到了问题,这里有一种解决方法,可以参考以下代码:
void CServer::handleGPS(AsyncWebServerRequest *request)
{
request->send(SPIFFS, "/gps.htm", String(), false, [this](const String &var) { return processor(var); });
}
通过使用 lambda 表达式,你可以将 processor
函数传递给 request->send
,并且正确地将参数传递给 processor
函数。
这个 lambda 表达式 [this](const String &var) { return processor(var); }
接受一个参数 var
,然后调用你的 processor
函数来处理它。
希望这可以帮助你解决问题。如果有其他问题,请随时提问。
英文:
So I'm trying to enlist the use of ESPAsyncWebServer for a project, and due to the size of said project I'm refactoring my code into a bunch of different libraries. This is to be part of the Client Server handling section.
I've determined how to effectively pass the *request to separate functions for the purpose of cleaning my code up. I.E.
Within my Libraries .cpp file
server->on("/heap", HTTP_GET, [this](AsyncWebServerRequest *request) { handleHeap(request); });
which fires:
void CServer::handleHeap(AsyncWebServerRequest *request) { request->send(200, "text/plain", String(ESP.getFreeHeap())); }
Where I'm running into an issue is trying to pass in my "Processor" function to the response for templating.
String CServer::processor(const String &var)
{
if (var == "locationLat")
return String(gps.location.lat());
return String();
}
void CServer::handleGPS(AsyncWebServerRequest *request)
{
request->send(SPIFFS, "/gps.htm", String(), processor);
}
> error: no matching function for call to
> 'AsyncWebServerRequest::send(fs::FS&, const char [9], String, )
Trying to pass processor() in a Lambda also doesn't compile, because there isn't a parameter going in.
> error: no matching function for call
> to 'CServer::processor()
' request->send(SPIFFS, "/gps.htm", String(), [this] { processor(); });
> ^ lib\CServer\CServer.cpp:128:66: note:
> candidate is: lib\CServer\CServer.cpp:108:8: note: String
> CServer::processor(const String&)
sooo, how do I handle passing the required processor function, into my response parameters as instructed in the guide here?
For reference:
String processor(const String& var)
{
if(var == "HELLO_FROM_TEMPLATE")
return F("Hello world!");
return String();
}
// ...
//Send index.htm with template processor function
request->send(SPIFFS, "/index.htm", String(), false, processor);
Fine if its all in main.cpp, but that is not what I want to do!
答案1
得分: 8
由于您的处理器函数是一个类方法,您需要将其与隐式的 this
参数绑定。
我们可以找到回调类型定义:typedef std::function<String(const String&)> AwsTemplateProcessor;
在这里,lambda 表达式以 const String &var
作为参数,就像 AwsTemplateProcessor
一样,并且我们通过在 lambda 原型之后写 -> String
来明确指定 AwsTemplateProcessor
函数的返回类型。通过正确指定参数和返回类型,send 方法可以将 lambda 与 std::function
(AwsTemplateProcessor
)关联起来。
英文:
Since your processor function is a class method, you'll need to bind it with the implicit this
argument.
We can find the callback typedef : typedef std::function<String(const String&)> AwsTemplateProcessor;
request->send(SPIFFS, "/index.htm", String(), false, [this](const String &var) -> String { return this->processor(var); });
The lambda here is taking a const String &var
as parameter such as the AwsTemplateProcessor
, and we precise the return type of the AwsTemplateProcessor
function by writing -> String
after the lambda prototype. By precising the parameters and the return type correctly the send method can relate the lambda to the std::function
(AwsTemplateProcessor
).
答案2
得分: 1
将您的方法设为static
,或者调用应该是:
static void CServer::handleGPS(AsyncWebServerRequest *request)
{
request->send(SPIFFS, "/gps.htm", String(), [this](const String& var) { return processor(var); });
}
英文:
Make your method static
, or call should be:
void CServer::handleGPS(AsyncWebServerRequest *request)
{
request->send(SPIFFS, "/gps.htm", String(), [this](const String& var) { return processor(var); });
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论