Visual Studio代码c++;11分机警告 我在学习C++的过程中,我使用VisualStudio代码来进行MAC。我使用CodeRunner来运行我的程序。我的问题是,当我使用c++11中的一些东西(如“auto”)进行变量声明时,visual studio代码会给我这样的警告,但如果我尝试在Xcode或Eclipse上运行它,它不会: warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions] for(auto y: nstrVec)

Visual Studio代码c++;11分机警告 我在学习C++的过程中,我使用VisualStudio代码来进行MAC。我使用CodeRunner来运行我的程序。我的问题是,当我使用c++11中的一些东西(如“auto”)进行变量声明时,visual studio代码会给我这样的警告,但如果我尝试在Xcode或Eclipse上运行它,它不会: warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions] for(auto y: nstrVec),c++,macos,visual-studio-code,C++,Macos,Visual Studio Code,如有必要,这是程序: #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")

如有必要,这是程序:

#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;
}

对于每个来此问题寻求快速答案的人(像我一样):

<> >编译器命令应该编译程序主.CPP < /C> >最新的C++标准(C++ 17),并且应该清除上面所描述的警告消息:

g++-std=c++17-g main.cpp-o main


评论中多次提到这个问题,但我认为这个问题应该有一个固定的答案。

我也有同样的问题,但使用set-vscode用户设置解决了它


今天我花了很长时间试图弄清楚为什么会出现这个错误,而我所需要的确切答案又不在哪里,所以我想我应该把它贴在这里,以防万一我可以省去任何人的麻烦

如果您使用的是code runner,请查看用户设置并设置:

 "code-runner.executorMap" : { "cpp" : "cd $dir && g++ -std=c++17 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt" }
相关位为“g++-std=c++17”

这当然提供了您可以使用上面Daniel的解决方案在shell中编译程序,但不能使用VScode+和code runner。

在VS代码中:

文件>>首选项>>设置>>扩展名

查找C_Cpp>默认值:Cpp标准下拉菜单

将其设置为c++11


我用这个来解决我的问题。打开你的终端

猛击

zsh


您是为C++11编译的吗?听起来不像。
auto
关键字是在C++11中引入的,因此在此之前它被Visual Studio视为一种语言扩展。@CoryKramer的问题是关于VS代码,而不是VS。您告诉编译器使用两种不同的标准:您有“-std=C++17”和“-std=C++11”。您能从命令行编译它吗?也就是说,打开一个shell,转到该目录,然后键入
g++-std=c++17-g helloworld.cpp-o helloworld
:它能工作吗?我很高兴你能解决你的问题,但请不要编辑你的帖子,在标题中添加“已解决”,堆栈溢出的工作方式不同。正确的表达方式是一旦有答案就接受。你可以问@Bob_uuuuu他是否有兴趣发布一个;如果他不是,请自己贴出来接受。非常感谢。你在哪里找到这个文件?但这并不能解决我的问题。它仍然显示相同的警告
"clang.cxxflags": ["-std=c++14"]
 "code-runner.executorMap" : { "cpp" : "cd $dir && g++ -std=c++17 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt" }
echo "alias g++='g++ -std=c++17'" >> ~/.bashrc
source ~/.bashrc
echo "alias g++='g++ -std=c++17'" >> ~/.zshrc
source ~/.zshrc