MacOS 上 Visual Studio Code 中的 C++ - std::make_unique

标签 c++ macos visual-studio-code c++17 c++20

我运行的是 MacOS 10.15,在使用 Visual Studio 2019 重新启动到我的 BootCamp Win10 几个月后,我决定尝试使用 C++ 和 Code Runner 扩展的 VS Code。可悲的是,我很快就遇到了操作系统内置的过时版本 C++ 的麻烦(或者使用开发人员工具下载,我不知道)。

我正在尝试运行简单的 std::make_unique<T>但我得到的回应是

error: no member named 'make_unique' in namespace 'std'

这是我的代码:

#include <memory>
#include <vector>


class Pole{

    Pole(size_t chunk=100): chunk_(chunk), count_(0) {}

    public:
        void push_back(int item){

            if(count_ % chunk_ == 0){
                v_.push_back(std::make_unique< int[]>(chunk_));  //this is the problematic line

            }
            count_++;
            v_[count_/chunk_][count_%chunk_]=item;
        }



    private:
        size_t chunk_;
        size_t count_;

        std::vector<std::unique_ptr<int[]> > v_;

};

我已经尝试过 "cppStandard":c_cpp_properties.json并将其值设置为 "cppStandard": "c++20" , 但它没有帮助。

谢谢!

最佳答案

  1. 打开 Visual Studio Code。
  2. 打开项目的文件夹。
  3. 将下面的 .vscode/c_cpp_properties.json 编译器路径和属性替换为:

-

"intelliSenseMode": "clang-x64",
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17"

所以它看起来有点像:

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
            ],
            "intelliSenseMode": "clang-x64",
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c11",
            "cppStandard": "c++20"
        }
    ],
    "version": 4
}

接下来打开 ./vscode/tasks.json 并为 commandargs 指定:

"command": "clang++",
"args": [
    "-std=c++17",
    "-stdlib=libc++",
    "${workspaceFolder}/main.cpp",
    "-o",
    "${workspaceFolder}/main.out",
    "--debug"
]

两个重要的参数是:

"-std=c++17",
"-stdlib=libc++"

剩下的你可以随心所欲..

现在你应该能够为 C++17 构建。按 Command + Shift + B 构建,按 F5Fn + F5 运行/调试。

关于MacOS 上 Visual Studio Code 中的 C++ - std::make_unique,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59203035/

相关文章:

c++ - 在 C++ 中将整数移动到最接近的可被 4 整除的位置

c++ - 仅当 .h 位于特定目录中时,g++ 才会神秘地失败

javascript - [通告*1]是什么意思(VS代码)

python - vscode找不到python的requests库

terminal - 是否可以在 Visual Studio Code 启动时自动打开集成终端?

c++ - 在C++中设置节点的内存地址

c++ - Boost::Asio - 抛出 get_io_service 异常

c++ - 为什么我的 espeak-ng 程序什么也不说?

xcode - Swift:更改 NSbutton 的字体和样式

objective-c - 在 OpenGL objective c os x 中绘制纹理