我在
gcc下编译代码时遇到了一些奇怪的错误.它告诉我std :: function不存在.
- #include <functional>
- #include <stdio.h>
- void test(){ printf ("test"); }
- int main() {
- std::function<void()> f;
- f = test;
- f();
- }
如果我运行gcc(来自cygwin):(我的错误信息是德语,所以我翻译了它.在英语gcc上可能听起来不同)
- $gcc test.cpp
- test.cpp: in function "int main():
- test.cpp:7:3: Error: "function" is not an element of "std"«
- test.cpp:7:25: Error: "f" was not defined in this scope
使用MSVC,它编译成功.
请告诉我在我的代码中我做错了什么.
约翰内斯
解决方法
将其编译为:
- g++ test.cpp -std=c++0x
-std = c需要0x,因为你使用的是C 11特性,否则g test.cpp就足够了.
确保您拥有最新版本的GCC.您可以将版本检查为:
- g++ --version