I am trying to pass a lambda as parameter to a function but once I attempt to access a variable inside the lambda which was declared outside, the build fails: error: no matching function for call to 'AWS::subscribe(char [128], mainTask(void*)::<lambda(AWS_IoT_Client*, char*, uint16_t, IoT_Publish_Message_Params*, void*)>)'
I was thinking that the [&]
would take care of capturing variables. I also tried [=]
as well as [someVar]
, [&someVar]
.
I'm using C++11.
char someVar[128];
aws->subscribe(
topic,
[&] (AWS_IoT_Client *pClient, char *topicName, uint16_t topicNameLen, IoT_Publish_Message_Params *params, void *pData) {
char *text = (char *)params->payload;
sprintf(someVar, "%s", text);
}
);
From the AWS library:
void AWS::subscribe(const char *topic,
pApplicationHandler_t iot_subscribe_callback_handler) {
m_error =
::aws_iot_mqtt_subscribe(&m_client, topic, (uint16_t)std::strlen(topic),
QOS1, iot_subscribe_callback_handler, NULL);
}