简体   繁体   English

C ++ 2D数组-数组下标的错误无效类型'int [int]'

[英]C++ 2D Array - Error invalid types ‘int[int]’ for array subscript

I am trying to create MxN matrix using 2D-arrays in C++. 我正在尝试使用C ++中的2D数组创建MxN矩阵。

The createMatrix() function asks for user input for matrix items and the printMatrix() function has to print the matrix. createMatrix()函数要求用户输入矩阵项,而printMatrix()函数必须打印矩阵。

But the printing task is not working (I can't access the array created, I don't understand why) 但是打印任务无法正常工作(我无法访问创建的数组,我不明白为什么)

I receive the error : 我收到错误:

matrix.cpp:35:20: error: invalid types ‘int[int]’ for array subscript
    cout << matrix[i][j];

The code I'm working with is: 我正在使用的代码是:

#include "iostream"
using namespace std;

// user input matrix
int createMatrix(int m, int n){
    int arr[m][n];
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            cout << "A[" << i << "][" << j << "] : ";
            cin >> arr[i][j];
        }
        cout << endl;
    }
    return arr[m][n];
}

/*
void printMatrix(int matrix[][2], int m, int n){
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            cout << matrix[i][j];
        }
    }
}
*/

int main(){ 
    int m = 2, n = 2; // m = rows, n = columns
    int matrix = createMatrix(m,n);

    //  printMatrix(matrix, m, n); // not working as sub-routine too, main target to make it work with sub-routine

    // to print matrix
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            cout << matrix[i][j];
        }
    }

    return 0;
}

matrix is an int not an int[][] . matrix是一个int而不是int[][] Since it is an int there is no subscript operator and that is why you are getting the error you are getting. 由于它是一个int所以没有下标运算符,这就是为什么出现错误的原因。 You are also using veriable length arrays which is not standard C++. 您还使用了不是标准C ++的可验证长度数组。 I would suggest you change your code to use a std::vector like 我建议您将代码更改为使用std::vector

std::vector<std::vector<int>> createMatrix(int m, int n)
{
    std::vector<std::vector<int>> arr(m, std::vector<int>(n));
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            cout << "A[" << i << "][" << j << "] : ";
            cin >> arr[i][j];
        }
        cout << endl;
    }
    return arr;
}

And then main() would be: 然后main()将是:

int main(){ 
    int m = 2, n = 2; // m = rows, n = columns
    std::vector<std::vector<int>> matrix = createMatrix(m,n);

    //  printMatrix(matrix, m, n); // not working as sub-routine too, main target to make it work with sub-routine

    // to print matrix
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            cout << matrix[i][j];
        }
    }

    return 0;
}

Your matrix is not array. 您的矩阵不是数组。 it is int . 它是int

You need to work with the pointers. 您需要使用指针。

Yes, createMatrix works but you won't be able to do anything with what it created. 是的, createMatrix可以工作,但是您将无法对其创建的内容执行任何操作。 Because: 因为:

  1. arr[n][m] is local (and out of boundaries by the way). arr[n][m]是局部的(顺便说一句)。 It is not a matrix as you probably thought but an item of arr at position [n][m]. 它不是您可能想到的矩阵,而是位置[n] [m]处的arr项。
  2. It is not well defined to declare array of fixed sizes with vary sizes that depend on function input. 声明大小固定的固定大小的数组(取决于函数输入)的定义不充分。

You need to pass to createMatrix array from the main() as pointer (like you did in printMatrix ) and createMatrix should work with it and not something local. 您需要从main()作为指针传递createMatrix数组作为指针(就像在printMatrix所做的printMatrix ),并且createMatrix应该使用它而不是局部对象。

Now regarding your original question: 现在关于您的原始问题:

But the printing task is not working (I can't access the array created, I don't understand why) 但是打印任务无法正常工作(我无法访问创建的数组,我不明白为什么)

matrix was defined as int , not as array. matrix被定义为int ,而不是数组。

int matrix = createMatrix(m,n); int matrix = createMatrix(m,n);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM