无法声明二维向量

Can't declare a 2D vector

我得到 3 个错误:

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

我正在 Mac 在 VS Code 中使用 Clang 和 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
}

launch.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"
        }
    ]
}

tasks.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
        }
      }
    ]
  }

代码有效,runs(根据编译器警告,注意那些 signed/unsigned 比较!)。

您描述的错误消息表明您的编译器或 IDE 的“IntelliSense”等效项(取决于您看到错误消息的位置)不理解 C++11 中的统一初始化语法这个语境。

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

好吧,我找到了解决方案。

对于“预期的';'在声明结束时 [29, 43]" 错误:

VS Code IntelliSense 显示错误,但由于编译器没有发现任何错误,所以我的代码运行了。作为解决方案,我们可以忽略它,但它不会阻止编译过程。即使有时,错误也不会出现...这可能是 VS Code 的错误,因为我的 JSON 文件设置正确并且所有内容都是最新的。

对于无效操作数错误:

那是因为我们显然不能“算出”一个向量。所以为了打印一个向量,我们必须为它创建一个函数。

这是我的文件,一切正常。对于 JSON 文件,你可以复制我的,但你可能只需要更改一些路径:

main.cpp:

#include <iostream>
#include <vector>

void invert(std::vector< std::vector <int> > vector)
{
    /* In : a 2d vector containing only binaries.
    Out : the previous 2d vector with the binaries interchanged. */

    for (int i = 0; i < vector.size(); i++)
    {
        for (int j = 0; j < vector[i].size(); j++)
        {
            if (vector[i][j] == 0) vector[i][j] = 1;
            else vector[i][j] = 0;
            std::cout << vector[i][j] << " ";
        }
        std::cout << std::endl;
    }
}

void print2DVector(std::vector< std::vector <int> > vector)
{
    for (int i = 0; i < vector.size(); i++)
    {
        for (int j = 0; j < vector[i].size(); j++)
        {
            std::cout << vector[i][j] << " ";
        }
        std::cout << std::endl;
    }
}

int main()
{
    std::vector < std::vector <int> > vector 
    {
        {1, 0, 0, 0, 1},
        {1, 1, 0, 1, 1},
        {0, 1, 0, 1, 0}
    };
    
    print2DVector(vector);
    std::cout << "\nInverting...\n\n";
    invert(vector);
}

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
}

launch.json:

{
    "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"
        }
    ]
}

tasks.json:

{
    "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
        }
      }
    ]
  }

航站楼:

1 0 0 0 1 
1 1 0 1 1 
0 1 0 1 0 

Inverting...

0 1 1 1 0 
0 0 1 0 0 
1 0 1 0 1 

祝你的编码项目好运!