错误C2664:无法将参数1从'int [] []'转换为'int'并且错误C2087:缺少下标

时间:2016-01-17 12:27:05

标签: c++

简要说明;我尝试制作一个5 * 3矩阵,从用户那里获取值(而且在main方法中;而在其他方法中,我试图做一些计算以轻松获得2D数组的结果,这里是代码:

requirements.txt

每当我编译时,都会出现以下错误:

#include <iostream>
using namespace std;

int All_Matrix_sum( int, int, int);
int Diag_Matrix_sum( int, int, int);

int main (){
    int My_Matrix[5][3];
    for(int i=0; i <5 ; i++){
        for (int y=0; y <3; y++){
            cout << "Please enter the Value corresponds to the position of " << i << ", " << y << endl;
            cin >> My_Matrix[i][y]; // get values from the user
            }
        }
    cout << "the sumation of all the matrix elements is : " << All_Matrix_sum(My_Matrix, 5 , 3 );
    cout << "the sumation of the matrix's diagonal is : "<<   Diag_Matrix_sum(My_Matrix, 5 , 3 );
}



int Diag_Matrix_sum(int Matrix[][], int rows, int columns){ // this method calculates the sumation of the diagonal elements
    int sumD=0;
    for(int Row_numbers=0; Row_numbers <rows; Row_numbers++){
        for (int Column_numbers=0; Column_numbers <columns; Column_numbers++){
            if(Column_numbers==Row_numbers)
            sumD += Matrix[Row_numbers][Column_numbers];        
            }
        }
    return sumD;
}


int All_Matrix_sum(int Matrix[][], int rows, int columns){ // this method calculates the sumation of all the elements in such a matrix
    int sumA=0;
    for(int Row_numbers=0; Row_numbers <rows; Row_numbers++){
        for (int Column_numbers=0; Column_numbers <columns; Column_numbers++){
            sumA += Matrix[Row_numbers][Column_numbers];        
            }
        }
    return sumA;
}

2 个答案:

答案 0 :(得分:1)

RecyclerView.ViewHolder是非法的。 C样式数组声明符必须指定除最里面之外的所有维度。

另一个问题是int Diag_Matrix_sum(int Matrix[][], int rows, int columns){的定义与原型不匹配。

解决这个问题的一种方法是写:

Diag_Matrix_sum

将此功能放在template<int rows, int columns> int Diag_Matrix_sum(int (&Matrix)[rows][columns]) { // ... 之前,这样您就不需要原型了。 main需要类似的修复。

为了使您的代码更易于阅读,请考虑使用基于范围的for循环,甚至更好的标准算法(例如All_Matrix_sum)来汇总范围。 std::accumulate中的算法可以大大简化,只需要一个循环。

答案 1 :(得分:0)

这是您的代码的编辑版本:

#include <iostream>
using namespace std;
/* If your function parameter is array then you must declare 
And if you are using other than 1D array, you need to specify the dimension(s)
*/

int All_Matrix_sum( int[][3], int, int);  
int Diag_Matrix_sum( int[][3], int, int); 

int main (){
    int My_Matrix[5][3];
    for(int i=0; i <5 ; i++){
        for (int y=0; y <3; y++){
            cout << "Please enter the Value corresponds to the position of " << i << ", " << y << endl;
            cin >> My_Matrix[i][y]; // get values from the user
            }
        }
    cout << "the sumation of all the matrix elements is : " << All_Matrix_sum(My_Matrix, 5 , 3 );
    cout << "the sumation of the matrix's diagonal is : "<<   Diag_Matrix_sum(My_Matrix, 5 , 3 );
}



int Diag_Matrix_sum(int Matrix[][3], int rows, int columns){ // this method calculates the sumation of the diagonal elements
    int sumD=0;
    for(int Row_numbers=0; Row_numbers <rows; Row_numbers++){
        for (int Column_numbers=0; Column_numbers <columns; Column_numbers++){
            if(Column_numbers==Row_numbers)
            sumD += Matrix[Row_numbers][Column_numbers];
            }
        }
    return sumD;
}


int All_Matrix_sum(int Matrix[][3], int rows, int columns){ // this method calculates the sumation of all the elements in such a matrix
    int sumA=0;
    for(int Row_numbers=0; Row_numbers <rows; Row_numbers++){
        for (int Column_numbers=0; Column_numbers <columns; Column_numbers++){
            sumA += Matrix[Row_numbers][Column_numbers];
            }
        }
    return sumA;
}