c++ - unique_ptr 编译错误

标签 c++ linux gcc stl 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

关于c++ - unique_ptr 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9766568/

相关文章:

c++ - C++如何对负数进行按位 "or"运算?

c++ - 如何让同一个类的对象在同一个函数中做不同的事情?

c++ - 如何在表达式编译错误时将概念评估为 false

python - 在 RPi 启动后立即运行 python 脚本

linux - BASH grep 脚本

c++ - cppreference.com 中的示例无法使用 MSVC 进行编译

无法评论//代码块

c++ - GCC编译器在查找头文件时不搜索子目录

c++ - GNU 链接器 : errors if pointer in header file is declared NULL and/or extern

c - pthread_create 的 LD_PRELOAD 问题