我运行的是 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"
, 但它没有帮助。
谢谢!
最佳答案
- 打开 Visual Studio Code。
- 打开项目的文件夹。
- 将下面的
.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
并为 command
和 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
运行/调试。
关于MacOS 上 Visual Studio Code 中的 C++ - std::make_unique,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59203035/