我正在学习 c++ 并且我正在使用适用于 Mac 的 Visual Studio 代码。我使用 Code Runner 来运行我的程序。我的问题是,当我使用 c++11 中的“auto”之类的东西进行变量声明时,Visual Studio 代码会给我这样的警告,但是如果我尝试在 Xcode 或 Eclipse 上运行它,它不会:
warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
for(auto y: nstrVec)
如果有必要,这是程序:
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include <numeric>
#include <sstream>
int main(){
std::vector<std::string> nstrVec(10);
std::string str("I'm a string");
nstrVec[0] = str;
std::cout << str.at(0) << "\n";
std::cout << str.front() << " " << str.back() << "\n";
std::cout << "Length " << str.length() << "\n";
// copies all characters after the fourth
std::string str2(str, 4);
for(auto y: nstrVec)
if(y != "")
std::cout << y << "\n";
return 0;
}
这是 c_cpp_proprerties.json 文件:
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",
"/System/Library/Frameworks/Kernel.framework/Versions/A/Headers"
],
"defines": [],
"macFrameworkPath": [
"/System/Library/Frameworks",
"/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
原文由 BONANDRINI CARLO 发布,翻译遵循 CC BY-SA 4.0 许可协议
在 VS 代码中:
文件>>首选项>>设置>>扩展
找到 C_Cpp>Default:Cpp Standard 下拉菜单
将其设置为 c++11