I have a class method:
bool sendRequest(DataFieldList& requestParams,
DataFieldList& responseParams,
std::string context, void(*sendFailure)() = NULL);
The sendFailure is an optional function pointer, in my C++ class in one instance I have:
sendRequest(paramList, responseList, "Could not set background lighting control brightness limit", HBCMaxBrightnessLimitSendFailure);
HBCMaxBrightnessLimitSendFailure is declared in the class as a private method:
void HBCMaxBrightnessLimitSendFailure();
When I build this application I get error:
lightingsystemcomponent.cpp(969): error C3867: 'Lighting::SystemComponent_impl::HBCMaxBrightnessLimitSendFailure': function call missing argument list; use '&Lighting::SystemComponent_impl::HBCMaxBrightnessLimitSendFailure' to create a pointer to member
I also tried:
sendRequest(paramList, responseList, "Could not set background lighting control brightness limit", HBCMaxBrightnessLimitSendFailure());
This changed the error to:
lightingsystemcomponent.cpp(969): error C2664: 'bool Lighting::SystemComponent_impl::sendRequest(DataFieldList &, DataFieldList &,std::string,void (__cdecl *)(void))' : cannot convert argument 4 from 'void' to 'void (__cdecl *)(void)'
Expressions of type void cannot be converted to other types
I've had a bit of a move around and tried to implement the proposal put forward by Spencer, new prototype:
bool sendRequest(DataFieldList& requestParams,
DataFieldList& responseParams,
std::string context,
std::function<void()> sendFailure = NULL);
In the sendRequest function:
void LightingEngine::sendRequest(DataFieldList& requestParams,
DataFieldList& responseParams,
std::string context,
std::function<void()> sendFailure) {
if ( sendFailure != NULL ) {
sendFailure();
}
}
Of course this isn't all the code, just enough to illustrate the new layout and call.
When I try to build this I get:
error C2064: term does not evaluate to a function taking 0 arguments
Here is an example of the class thats calling this routine and the callback function I'm passing:
void component::onFailure() {
...Do something...
}
void component::update() {
... Some set-up to build paramlist ...
LightingEngine* engine = getLightingEngine();
assert(engine != nullptr);
engine->sendRequest(¶mList, &responseList, "Some context text", [this]()->void { onFailure(); });
}