【发布时间】:2018-12-05 09:45:03
【问题描述】:
我正在学习 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
}
【问题讨论】:
-
你在为 C++11 编译吗?听起来不像。
auto关键字是在 C++11 中引入的,因此在此之前它被 Visual Studio 视为语言扩展。 -
@CoryKramer 问题是关于 VS Code,而不是 VS。
-
您告诉编译器使用 2 个不同的标准:您同时拥有“-std=c++17”和“-std=c++11”。
-
你能从命令行编译它吗?也就是说,打开一个shell,进入那个目录,然后输入
g++ -std=c++17 -g helloworld.cpp -o helloworld:它有用吗? -
很高兴您能解决您的问题,但请不要编辑您的帖子以在标题中添加“已解决”,Stack Overflow 的工作方式不同。表示它的正确方法是接受一个答案,一旦有答案。你可以问@Bob__ 他是否有兴趣发布一个;如果他不是,请自己发布并接受。谢谢!
标签: c++ macos visual-studio-code