首页 > 解决方案 > 无法声明 2D 矢量

问题描述

我收到 3 个错误:

我不明白为什么,我的代码似乎是正确的。我已经尝试过使用一维向量,但我得到了同样的错误。对我来说,我的语法是正确的。对于运营商的问题,我已经看到了很多关于它的主题,但没有人能帮助我。

我在 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
        }
      }
    ]
  }

标签: c++macosvector2d-vector

解决方案


该代码是有效的,并且可以运行(尽管根据编译器警告,请注意那些有符号/无符号的比较!)。

您描述的错误消息表明您的编译器或 IDE 的“IntelliSense”等效项(取决于您在哪里看到错误消息)在这种情况下不理解 C++11 的统一初始化语法。

确保所有 C++ 语言设置都是“正确的”,并且您环境中的所有工具都是最新的。


推荐阅读