20

如果我告诉你我无法编译它,我想这会很尴尬。你能帮我吗:

#include<memory>
using namespace std;

int  main()
{
    std::unique_ptr<int> p1(new int(5));
    return 0;
}
$ gcc main.cpp 
main.cpp: In function ‘int main()’:
main.cpp:6:2: error: ‘unique_ptr’ was not declared in this scope
main.cpp:6:13: error: expected primary-expression before ‘int’
main.cpp:6:13: error: expected ‘;’ before ‘int’

$ gcc --version
gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
4

2 回答 2

32

这只是一个猜测。

您很可能像这样(或类似)编译了您的程序:

g++ main.cpp

如果你这样做了,那么问题是 g++ 默认使用 c++03。要使用 c++11 功能(和std::unique_ptr),您需要使用较新版本的 c++:

g++ -std=c++11

或者

g++ -std=c++14

我建议也使用-Wall -Wextra -pedantic.

于 2012-03-19T07:48:54.193 回答
4

如果您正在使用Code::Blocks,请转到Settings > Compiler > Global compiler settings > Compiler settings并查找Have g++ follow the C++11 ISO C++ language standard [ -std=c++11]并检查它!

( Code::Blocks-std=c++11在编译时为你添加)

于 2015-08-21T14:51:56.820 回答