为什么c++编译器找不到unique_ptr

Why g++ compiler is not able to find unique_ptr?

本文关键字:ptr unique 找不到 c++ 编译器 为什么      更新时间:2023-10-16

我正在尝试编译一个涉及unique_ptr的小c++代码,如下所示。

#include <iostream>
#include <memory>
using namespace std;
int main()
{
    unique_ptr<int> p1(new int);
}

当我尝试使用g++编译代码时,它抛出'unique_ptr'未在此范围内声明。我试着在Linux上编译。甚至我尝试使用'-std=c++11'选项。它说'无法识别的命令行选项-std=c++11'。有谁能告诉我怎么解决这个问题吗?

你需要包含它,它来自<memory>

#include <memory>

根据GCC 4.4发布说明,在4.4之前,unique_ptr并不在GCC的标准c++库中。

所以你可能想先检查你的GCC版本,使用g++ --version像@40two说。