2014-11-21 80 views
0

編輯:行和列INT值,ROW = 12,COLUMN = 2C++錯誤傳遞一個二維數組的功能?

int main() { 
    double list[ROW][COLUMN]; 

    ifstream inFile; 
    getValidDataFile(inFile); 
    cout << "Temperature Data for the year " << getYear(inFile) << endl; 

    getData(inFile, list[][COLUMN], ROW); // Error Line 


    return 0; 
} 

錯誤: 我需要從文件中獲得的數據: 「錯誤之前 ']' 令牌預期主表達式」並將其存儲在二維數組中。 BTW,這是一個家庭作業

void getData(ifstream& fin, double a[][COLUMN], int ROW) { 
    int row, col; 
    double num; 
    for(row = 0; row < ROW; row++) { 
     for(col = 0; col < COLUMN; col++) { 
      fin >> num; 
      a[row][col] = num; 
     } 
    } 
} 
+0

'ROW' /'COLUMN'是究竟?請提供代碼和錯誤,請提供[MCVE](http://stackoverflow.com/help/mcve)! – 2014-11-21 02:15:37

回答

-2

它會更容易把它作爲與ROW和COL信息的雙指針。所以,你的代碼將

void getData(ifstream *fin, double** a, int row, int col); 

函數定義將保持不變

getData(inFile, list, row, col); 

是使用它的方式。

+0

我想這樣做,但我的課程經理給了我們關於如何構建我們的功能的具體說明。 – 2014-11-21 02:18:05

2

當你調用getData()時,你應該傳入數組而不指定尺寸。申報清單後,[X] [Y]將在行X列Y.訪問單個元素

getData(inFile, list, row); 

此外,建議只使用大寫字母宏,而不是功能參數:

void getData(ifstream& fin, double a[][COLUMN], int input_row) { 
0

你可以提列大小的最大尺寸,同時聲明和定義的功能,像往常一樣,將數組的基址的功能

void print(int p_arr[][10]); //max size of the column -- declaration 
int g_row,g_column;//make it as these variables as global; 
int main() 
{ 
    int l_arr[10][10];//local array 
    printf("Enter row value and column value"); 
    scanf("%d%d",&g_row,&g_column); 
    for(int i=0;i<g_row;i++) 
    { 
     for(int j=0;j<g_column;j++) 
     { 
     scanf("%d",&l_arr[i][j]); 
     } 
    } 
    print(l_arr);//you just pass the array address to the function 
    return 0; 
} 
void print(int p_arr[][10]) 
{ 
    for(int i=0;i<g_row;i++) 
    { 
     for(int j=0;j<g_column;j++) 
     { 
     printf("%d\t",p_arr[i][j]); 
     } 
     printf("\n"); 
    } 
    return; 
}