c++ - 错误 : ‘unique_ptr’ is not a member of ‘std’

标签 c++ c++11 g++ unique-ptr

我想这是不言自明的 - 我似乎无法使用 C++11 功能,即使我认为我已经正确设置了所有内容 - 这可能意味着我没有。

这是我的代码:

#include <cstdlib>
#include <iostream>

class Object {
    private:
        int value;

    public:
        Object(int val) {
            value = val;
        }

        int get_val() {
            return value;
        }

        void set_val(int val) {
            value = val;
        }
};

int main() {

    Object *obj = new Object(3);
    std::unique_ptr<Object> smart_obj(new Object(5));
    std::cout << obj->get_val() << std::endl;
    return 0;
}

这是我的 g++ 版本:

ubuntu@ubuntu:~/Desktop$ g++ --version
g++ (Ubuntu/Linaro 4.7.3-2ubuntu1~12.04) 4.7.3
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

我是这样编译代码的:

ubuntu@ubuntu:~/Desktop$ g++ main.cpp -o run --std=c++11
main.cpp: In function ‘int main()’:
main.cpp:25:2: error: ‘unique_ptr’ is not a member of ‘std’
main.cpp:25:24: error: expected primary-expression before ‘>’ token
main.cpp:25:49: error: ‘smart_obj’ was not declared in this scope

请注意,我尝试了 -std=c++11-std=c++0x 均无济于事。

我正在英特尔 x64 机器上的闪存驱动器上运行 Ubuntu 12.04 LTS。

最佳答案

您需要在 unique_ptr 处包含标题和 shared_ptr已定义

#include <memory>

你已经知道你需要使用 c++11 标志进行编译

g++ main.cpp -o run -std=c++11
//                  ^

关于c++ - 错误 : ‘unique_ptr’ is not a member of ‘std’ ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18078153/

相关文章:

c++ - 样条拟合 - Douglas-Peucker 算法

c++ - 为什么以及何时需要使用#pragma

c++ - 结构类型转换

c++ - 使用函数参数作为常量表达式的一部分 - gcc vs clang

C++ 链接 Lapack

g++ - 在 qmake 命令行中定义一个字符串

c++ - gcc 在链接时找不到对函数 DoIt() 的引用

c++ - 从 3 个数组中查找最大前 5 个数的优雅代码

c++ - 如何使用 initializer_ist 在构造函数中初始化动态数组?

c++ - mingw g++ 4.7.2 中未声明的 decltype