2014-02-19 120 views
-3
#include<iostream> 
using namespace std; 
double NUMB_COLS = 3; 

void readMatrix(int matr[] [3], int numb_rows, int numb_cols) 
{ 
    for (int i = 0; i<numb_rows; i++) 
    { 
     for (int j = 0; j < numb_cols; j++) 
     { 
      cout <<"[" << i << "] [" <<j <<"] ="; 
      cin >> matr[i][j]; 
     } 
    } 
} 
void printMatr(int matr[][3], int numb_rows, int numb_cols) 
{ 
    for (int i = 0; i < numb_rows; i++) 
    { 
     for (int j = 0; j < numb_cols; j++) 
     { 
      cout << "[" << i << "][" << j << "] = " << matr[i][j]; 
     } 
      cout << endl; 
    } 
} 
int main() 
{ 
    int matr[5][10]; 
    printMatr(matr, 4, 5); 
    readMatrix(matr, 4, 5); 
return 0; 
} 

该错误是二维阵列误差

31 23 C:\ Users \用户的Windows \桌面\程序\ arrays2.cpp [错误]不能转换 'INT()[10]' 至'INT()[3]' 的参数 '1' 到 '空隙readMatrix(INT(*)[3],INT,INT)'

做什么?

+0

使用'std :: vector'。 –

回答

1

您需要指定正确的下标。

void readMatrix(int matr[5][10], int numb_rows, int numb_cols) 

std :: vector在这种情况下会更容易。

1

第一个错误是,您将指向matr的指针传递给期望不同阵列布局的函数。请记住,matr中的所有int在内存中都是连续的。即使在将它传递给函数之前将matr转换为预期类型,中的位置matr[0][7]将在readMatrix()内的位置matr[2][1]处结束。

第二个错误是,即使您的函数已经声明列计数为3,您的函数也接受列计数。这种不一致是错误的充足根源,应该不惜一切代价予以避免。不幸的是,C++禁止你使用动态调整大小的数组类型,所以你不能只是改变你的函数声明

void readMatrix(int numb_rows, int numb_cols, int matr[numb_rows][numb_cols]) { 

,你可以在C.

解决您的问题最简单的方法可能是使用std::vector<>

+0

你可以这样做:'