c++ - MacOS 上 Visual Studio Code 中的 C++ - std::make_unique
问题描述
我正在运行 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":
inc_cpp_properties.json
并将其值设置为"cppStandard": "c++20"
,但它没有帮助。
谢谢!
解决方案
- 打开 Visual Studio 代码。
- 打开项目的文件夹。
- 将
.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
and for command
and args
,指定:
"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
构建和/F5
或Fn + F5
运行/调试。