c – 函数不是std的元素

前端之家收集整理的这篇文章主要介绍了c – 函数不是std的元素前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在 gcc下编译代码时遇到了一些奇怪的错误.它告诉我std :: function不存在.

我可以使用以下代码重新创建错误

  1. #include <functional>
  2. #include <stdio.h>
  3.  
  4. void test(){ printf ("test"); }
  5.  
  6. int main() {
  7. std::function<void()> f;
  8. f = test;
  9. f();
  10. }

如果我运行gcc(来自cygwin):(我的错误信息是德语,所以我翻译了它.在英语gcc上可能听起来不同)

  1. $gcc test.cpp
  2. test.cpp: in function "int main():
  3. test.cpp:7:3: Error: "function" is not an element of "std
  4. test.cpp:7:25: Error: "f" was not defined in this scope

使用MSVC,它编译成功.
请告诉我在我的代码中我做错了什么.

约翰内斯

解决方法

将其编译为:
  1. g++ test.cpp -std=c++0x

-std = c需要0x,因为你使用的是C 11特性,否则g test.cpp就足够了.

确保您拥有最新版本的GCC.您可以将版本检查为:

  1. g++ --version

猜你在找的C&C++相关文章