C++代码在VS 2017中执行,但不在VSCode中执行
C++ code executes in VS 2017 but does not in VSCode
我有这个简单的C++代码。它在Microsoft Visual Studio 2017中正确执行。
// Author: Herbert Schildt
// Modified: Qiang Hu
// File name: ~ftp/pub/class/cplusplus/Array/Array3.cpp
// Purpose: Use sqrs[][] -- two dimentional integer
// array to store integers from 1 to 10 and
// their squares.
// Ask user for a number, then look up this
// number in the array and then print out its
// corresponding square.
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// C++ allows for the initialization of arrays. In the following,
// we are initializing a 10x2 integer array. After initialization,
// sqrs[0][0] = 1
// sqrs[0][1] = 1
// sqrs[1][0] = 2
// sqrs[1][1] = 4
// and so on
vector<vector<int> > sqrs = { {1, 1},
{2, 4}, // The square of 2 is 4,and so on
{3, 9},
{4, 16},
{5, 25},
{6, 36},
{7, 49},
{8, 64},
{9, 81},
{10, 100}
};
int i, j;
cout << "Enter a number between 1 and 10: ";
cin >> i;
// look up i
for(j = 0; j < 10; j++)
if(sqrs[j][0] == i) break; // break from loop if i is found
cout << "The square of " << i << " is " ;
cout << sqrs[j][1] << endl;
return 0;
}
我尝试在Windows下使用VSCode来编译相同的代码,但代码没有执行。它给了我以下错误。
Untitled-2.cpp: In function 'int main()':
Untitled-2.cpp:34:19: error: in C++98 'sqrs' must be initialized by constructor, not by '{...}'
};
^
Untitled-2.cpp:34:19: required from here
Untitled-2.cpp:34:19: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
我猜VSCode使用的编译器不是C++11,而是C++98。如果是这种情况,如何将编译器更改为 C++11。(在这里,我正在寻找解决方案,但从未找到有用的东西。如果没有,如何解决这个问题?
在 tasks.json 文件中,您可以像这样为编译器指定参数:
{
"version": "0.1.0",
"command": "g++",
"isShellCommand": true,
"args": ["-std=c++11", "example.cpp"], //<- here you can specify c++ version as well as any other options
...
}
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- C++,系统无法执行指定的程序
- 使用C++中的模板和运算符重载执行矩阵运算
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 执行函数时导致崩溃的变量
- 无论条件是否为true,if总是在c++中执行
- 当函数模板参数是具有默认参数的类模板时,函数模板参数的推导如何执行
- vscode g++链路故障:体系结构x86_64的未定义符号
- 在C++中对T*类型执行std::move的意外行为
- 使用QProcess执行命令,并将结果存储在QStringList中
- 如何在没有信号的情况下从C++执行QML插槽
- 如何确认我的constexpr表达式实际上已经在编译时执行
- VSCode-有一个红色下划线,但程序构建和运行正确,并且出现配音错误
- C++17中的并行执行策略
- 当使用带有VS2019或VSCode的虚幻引擎4.24.2时,我如何修复这些错误的Intellisense错误
- QML按钮点击功能执行顺序
- 程序在执行程序的其余部分之前退出
- 如何在 VSCODE 中构建、运行和删除可执行文件和虚拟文件
- C++代码在VS 2017中执行,但不在VSCode中执行
- 当我执行C ++程序时如何设置VSCode屏幕不会立即消失