将2D数组引入函数c ++

时间:2015-07-07 09:08:57

标签: c++ arrays function

大家早上好,我知道有很多形式可以将一个数组传递给一个函数,而且有很多关于这个的东西。但即使阅读这篇文章我也无法将我的数组传递给函数。 当我在main上调用该函数时,它会出现错误,如下面的代码所示。

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int row = 5;
int column=20;
void dimensions (int [20]);

int main (){

int *matriuprova[column];
for (int i= 0; i<column;i++){

    matriuprova[i] = new int [row];

    dimensions(matriuprova);// <-- here is the error:
        //main.cpp:14:31: error: cannot convert 'int**' to 'int*' 
        //for argument '1' to 'void dimensions(int*)'

}//end of for loop

return 0;
}//end of main

void dimensions (int *matriuprova [20]){
        //function code
         }

我已关注此链接: Passing a 2D array to a C++ function

感谢大家!

3 个答案:

答案 0 :(得分:2)

你有dimensions的错误前瞻声明。

更改

    void dimensions (int [20]);

    void dimensions (int* [20]);

答案 1 :(得分:1)

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int row = 5;
int column=20;
void dimensions (int* [20]);

int main (){

int *matriuprova[column];
for (int i= 0; i<column;i++){

    matriuprova[i] = new int [row];

    dimensions(matriuprova);// <-- here is the error:
        //main.cpp:14:31: error: cannot convert 'int**' to 'int*' 
        //for argument '1' to 'void dimensions(int*)'

}//end of for loop

return 0;
}//end of main

void dimensions (int *matriuprova [20]){
        //function code
         }

就像编译器说的那样。它找到了void dimensions(int [20])的定义,它与您的矩阵不匹配。 纠正定义并解决问题。

答案 2 :(得分:-1)

你试过吗

a b 
a c 
b d 
c e