c++ - 无法声明 2D 矢量
问题描述
我收到 3 个错误:
- 首先,我想声明一个 2D 向量,但出现错误:预期的 ';' 在声明结束时 [29, 43];
- 我收到错误:二进制表达式的无效操作数 ('std::__1::ostream' (aka 'basic_ostream') 和 'std::vectorstd::vector<int >') [39, 5] ;
- 第三个错误:没有运算符“<<”与这些操作数匹配——操作数类型为:std::__1::ostream << std::__1::vector<std::__1::vector<int, std::__1: :allocator>, std::__1::allocator<std::__1::vector<int, std::__1::allocator>>> [39, 15]。
我不明白为什么,我的代码似乎是正确的。我已经尝试过使用一维向量,但我得到了同样的错误。对我来说,我的语法是正确的。对于运营商的问题,我已经看到了很多关于它的主题,但没有人能帮助我。
我在 Mac 上使用 Clang 和 VS Code 中的 C++20。我检查了我的设置、配置等,一切似乎都是最新且正确的。
有人可以解释一下这里有什么问题吗?
#include <iostream>
#include <vector>
std::vector < std::vector <int> > interchange(std::vector< std::vector<int> > vect)
{
/* In : a 2d vector containing only binaries.
Out : the previous 2d vector with the binaries interchanged. */
for (int i = 0; i < vect.size(); i++)
{
for (int j = 0; j < vect[i].size(); j++)
{
if (vect[i][j] == 0)
{
vect[i][j] = 1;
}
else
{
vect[i][j] = 0;
}
}
}
return vect;
}
int main()
{
std::vector < std::vector <int> > vect
{
{1, 0, 1},
{1, 1, 1},
{0, 0, 1}
};
std::vector < std::vector <int> > a = interchange(vect);
std::cout << a << std::endl; // making sure that my function works
std::cout << "Hello World!\n" << "This is my program!\n"; // making sure that the program runs
}
.vscode 配置文件:
c_cpp_properties.json:
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"cppStandard": "c++20"
}
],
"version": 4
}
启动.json:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "clang++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "clang++ build active file"
}
]
}
任务.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++2a",
"-stdlib=libc++",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
解决方案
该代码是有效的,并且可以运行(尽管根据编译器警告,请注意那些有符号/无符号的比较!)。
您描述的错误消息表明您的编译器或 IDE 的“IntelliSense”等效项(取决于您在哪里看到错误消息)在这种情况下不理解 C++11 的统一初始化语法。
确保所有 C++ 语言设置都是“正确的”,并且您环境中的所有工具都是最新的。
推荐阅读
- javascript - 如何在js中创建文件对象?
- c# - 如何通过删除 if 检查使代码面向对象?
- ios - 如何在 swift 中从两天中获取日期名称
- python - 如何确保快速可靠地将数据集迁移到固件后端?
- angular - 如何将多个图像文件从快递服务器发送到客户端?
- typescript - 使用未定义的显式返回作为类型保护和/或过滤器?
- qt - 在 Qt 中,forever 关键字有什么实际用途吗?
- javascript - 如何在 Kentico 中为电子邮件模板每三个表格单元格包装一行?
- javascript - 使用名称和坐标传单找到某个地方
- excel - VBA:在 IE 中搜索文本,打开链接,查找行复制文本,在 Excel 中插入