2014-02-19 117 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

你可以這樣做:'