我正在尝试运行一个简单的lambda示例。
// lambda.cpp
#include <functional>
//#include <tr1/functional>
int main()
{
// Assign the same lambda expression to a function object.
function<int (int, int)> f2 = [] (int x, int y) { return x + y; };
//function<int (int, int)> f2 = [] (int x, int y) { return x + y; };
}
我正在编译它:
$ g++ -std=c++0x -fpermissive lamdas.cpp
lambdas.cpp: In function ‘int main()’:
lambdas.cpp:10: error: expected primary-expression before ‘=’ token
lambdas.cpp:10: error: expected primary-expression before ‘[’ token
lambdas.cpp:10: error: expected primary-expression before ‘]’ token
lambdas.cpp:10: error: expected primary-expression before ‘int’
lambdas.cpp:10: error: expected primary-expression before ‘int’
lambdas.cpp:10: error: expected ‘;’ before ‘{’ token
如何在没有错误的情况下编译它?
答案 0 :(得分:5)
您的意思是std::function
吗?
标准库功能位于std
命名空间中。
你的复制/粘贴显然是假的也很有趣;你写了“lamdas.cpp”,然后编译了“lambdas.cpp”!
答案 1 :(得分:2)
std::function<int (int, int)> f2 = [] (int x, int y) { return x + y; };
或者,可能更好
auto f2 = [] (int x, int y) { return x + y; };
答案 2 :(得分:0)
在我看来你忘记了-std = c ++ 0x。