unique_ptr编译错误

如果我告诉你我不能把它编译出来,我觉得这很尴尬。 你能帮我吗?

#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

如果您使用的是Code :: Blocks ,请转至设置>编译器>全局编译器设置>编译器设置,然后查找Have g ++遵循C ++ 11 ISO C ++语言标准[ -std=c++11 ]并检查!

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