Linux unique_ptr 编译错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9766568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:15:09  来源:igfitidea点击:

unique_ptr compile error

c++linuxgccstlunique-ptr

提问by rahman

I guess this is embarrassing if I told you I cant get this to compile. would you please help me:

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

#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

采纳答案by B?ови?

This is just a guess.

这只是一个猜测。

Most likely you compiled your program like this (or similarly) :

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

g++ main.cpp

If you did, then the problem is that g++ uses c++03 as default. To use c++11 features (and std::unique_ptr), you need to use newer version of c++ :

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

g++ -std=c++11

or

或者

g++ -std=c++14

and I would recommend to use also -Wall -Wextra -pedantic.

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

回答by Sevener

If you are using Code::Blocks, go to Settings > Compiler > Global compiler settings > Compiler settingsand look for the Have g++ follow the C++11 ISO C++ language standard [-std=c++11]and check it!

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

(Code::Blockswill add the -std=c++11for you when compiling)

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