我在gcc下编译代码时遇到了一些奇怪的错误.它告诉我std::function不存在.
我可以使用以下代码重新创建错误:
#include <functional>
#include <stdio.h>
void test(){ printf ("test"); }
int main() {
std::function<void()> f;
f = test;
f();
}
Run Code Online (Sandbox Code Playgroud)
如果我运行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
Run Code Online (Sandbox Code Playgroud)
使用MSVC,它编译成功.请告诉我在我的代码中我做错了什么.
约翰内斯
Naw*_*waz 15
将其编译为:
g++ test.cpp -std=c++0x
Run Code Online (Sandbox Code Playgroud)
-std=c++0x因为你正在使用C++ 11功能所以需要,否则g++ test.cpp就足够了.
确保您拥有最新版本的GCC.您可以将版本检查为:
g++ --version
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11167 次 |
| 最近记录: |