如果我告诉你我无法编译它,我想这会很尴尬。你能帮帮我吗:
#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
最佳答案
这只是猜测。
您很可能像这样(或类似)编译了您的程序:
g++ main.cpp
如果你这样做了,那么问题是 g++ 默认使用 c++03。要使用 c++11 功能(和 std::unique_ptr
),您需要使用较新版本的 c++:
g++ -std=c++11
或
g++ -std=c++14
我也建议使用 -Wall -Wextra -pedantic
。
关于c++ - unique_ptr 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9766568/