【发布时间】:2015-03-21 21:55:27
【问题描述】:
我在确定 lambda 函数定义时遇到了问题。所以我有这段代码,它工作正常:
auto fnClickHandler = [](Button *button) -> void
{
cout << "click" << endl;
};
button->setEventHandler(MOUSEBUTTONUP, fnClickHandler);
但是我需要在 fnClickHandler 中使用闭包,所以我执行以下代码:
int someParam = 1;
auto fnClickHandler = [someParam](Button *button) -> void
{
cout << "click" << someParam << endl;
};
button->setEventHandler(MOUSEBUTTONUP, fnClickHandler);
现在我得到以下编译错误:
no matching function for call to ‘Button::setEventHandler(BUTTON_EVENT_HANDLERS, nameOfFunctionWhichHostsThisCode::__lambda0&)’|
Button::setEventHandler 函数是这样定义的:
void setEventHandler(int, void (*handler)(Button *));
我想我需要更改该定义以支持 lambda 闭包参数(可选),但到目前为止我失败了。你能帮我弄清楚吗?
谢谢!
【问题讨论】: