【发布时间】:2015-09-25 03:46:14
【问题描述】:
//==== 1 ====
string func1(string x1, string x2){
return x1 + x2;
}
auto lambda1 = [](string x1, string x2){cout << func1(x1,x2);};
//==== 2 ====
class Test{
public:
string func2(string x1, string x2){
return x1 + x2;
}
void tst(){
auto lambda2 = [](string x1, string x2){cout << func2(x1,x2);};
}
};
lambda1 是对的。 但是 lambda2 出错了(在 g++ 4.8 下):
error: 'this' was not captured for this lambda function
auto lambda2 = [](string x1, string x2){cout << func2(x1,x2);};
在 lambda 中调用成员函数的正确方法是什么?
【问题讨论】:
-
错误提到捕获
this。阅读 lambda 捕获是一个好的开始。 -
请不要将答案放在问题中 - 将其发布为回答您的问题。
-
答案在下面的框中。您可以回答自己的问题。