【发布时间】:2017-01-16 17:14:50
【问题描述】:
我正在尝试在我的 linux 机器(运行 gcc 版本 5.4.0)上的 c++ 中使用一个非常简单的 lambda 函数,但出现错误。
代码如下:
#include <iostream>
int main()
{
auto sayHello = [](int a, int b) -> int
{
return a + b;
};
sayHello(55, 66);
return 0;
}
g++ main.cpp -o listener && ./listener -std=c++11
这在 Windows 上的 Visual Studio 中有效,但即使 lambdas 是 supported on gcc 4.5 and higher 我得到错误:
main.cpp: In function ‘int main()’:
main.cpp:5:8: error: ‘sayHello’ does not name a type
auto sayHello = [](int a, int b) -> int
^
main.cpp:10:12: error: ‘sayHello’ was not declared in this scope
sayHello();
谁能告诉我如何解决这个问题?谢谢
【问题讨论】:
-
如果您阅读链接的问题和答案,您会注意到他们提到了
-std=c++11标志。弄清楚它的作用作为练习。 -
我建议关闭,因为这是一个微不足道的错字/错误。
-
@Walter 我认为这是个好主意。