我最近开始学习C++,使用Visual Studio Code,在用std::vector
声明一个2D矩阵的时候,我开始调用一个Python non-aggregate type 'vector<vector<int> >' cannot be initialized with an initializer list
,我上网看别人是怎么解决这个问题的,他们说这种错误在使用C11以前的C版本时经常出现,并按照给出的指示指示VisualStudio代码在编译程序时使用参数-std=c++11
。然而,这并没有改变任何事情,我现在不知道如何修复这个错误。
代码:
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<vector<int> > board = {
{0,0,3, 0,9,2, 0,0,0},
{4,0,0, 0,3,0, 0,1,0},
{2,7,0, 0,0,0, 0,0,0},
{0,1,0, 3,0,0, 0,0,8},
{0,5,0, 1,6,7, 0,3,0},
{3,0,0, 0,0,8, 0,6,0},
{0,0,0, 0,0,0, 0,5,3},
{0,3,0, 0,8,0 ,0,0,9},
{0,0,0, 6,2,0, 1,0,0}
};
vector<vector<int> > get_square(int x, int y, vector<vector<int> > grid) {
int norm_x = floor(x / 3) * 3;
int norm_y = floor(y / 3) * 3;
vector<vector<int> > square = {};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
square[i][j] = grid[i][j];
}
}
return square;
};
int main() {
int test1[2][2] = {
{1,2},
{3,4}
};
int test2[2][2] = {
{0,0},
{0,0}
};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
test2[i][j] = test1[i][j];
}
}
test2[0][0] = 0;
cout << test1 << "\n";
cout << test2 << "\n";
vector<vector<int> > result;
result = get_square(1,1,board);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << result[i][j] << ' ';
}
cout << "\n";
}
return 0;
};
设置. json(code-runner.executorMap
):
"code-runner.executorMap": {
"javascript": "node",
"java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
"c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"cpp": "cd $dir && g++ -std=c++11 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"objective-c": "cd $dir && gcc -framework Cocoa $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"php": "php",
"python": "python -u",
"perl": "perl",
"perl6": "perl6",
"ruby": "ruby",
"go": "go run",
"lua": "lua",
"groovy": "groovy",
"powershell": "powershell -ExecutionPolicy ByPass -File",
"bat": "cmd /c",
"shellscript": "bash",
"fsharp": "fsi",
"csharp": "scriptcs",
"vbscript": "cscript //Nologo",
"typescript": "ts-node",
"coffeescript": "coffee",
"scala": "scala",
"swift": "swift",
"julia": "julia",
"crystal": "crystal",
"ocaml": "ocaml",
"r": "Rscript",
"applescript": "osascript",
"clojure": "lein exec",
"haxe": "haxe --cwd $dirWithoutTrailingSlash --run $fileNameWithoutExt",
"rust": "cd $dir && rustc $fileName && $dir$fileNameWithoutExt",
"racket": "racket",
"scheme": "csi -script",
"ahk": "autohotkey",
"autoit": "autoit3",
"dart": "dart",
"pascal": "cd $dir && fpc $fileName && $dir$fileNameWithoutExt",
"d": "cd $dir && dmd $fileName && $dir$fileNameWithoutExt",
"haskell": "runhaskell",
"nim": "nim compile --verbosity:0 --hints:off --run",
"lisp": "sbcl --script",
"kit": "kitc --run",
"v": "v run",
"sass": "sass --style expanded",
"scss": "scss --style expanded",
"less": "cd $dir && lessc $fileName $fileNameWithoutExt.css",
"FortranFreeForm": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"fortran-modern": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"fortran_fixed-form": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"fortran": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
},
终端错误输出:
Build finished with errors(s):
path/to/file/sudoku.cpp:7:22: error: non-aggregate type 'vector<vector<int> >' cannot be initialized with an initializer list
vector<vector<int> > board = {
^ ~
path/to/file/sudoku.cpp:24:26: error: non-aggregate type 'vector<vector<int> >' cannot be initialized with an initializer list
vector<vector<int> > square = {};
^ ~~
2 errors generated.
The terminal process failed to launch (exit code: -1).
如果答案能为解决方案提供一个清晰的解释,我将不胜感激,因为我对C++没有太多的经验。
1条答案
按热度按时间nqwrtyyt1#
你试过改变你的
task.json
文件中的args
吗?它应该或多或少,看起来像这样。无论如何,如果你打算使用Visual Studio Code for C++,我建议你在WSL环境下进行C++工作,并通过终端编译。使用GDb调试可能会很乏味,但你可以使用https://www.onlinegdb.com/作为替代方案。