處理從文件中讀取整數並將它們放入二維數組的項目。我測試了一維數組,但是當我用二維數組嘗試它時,我一直在我的類中名爲「Image」的錯誤「數組下標」無效類型int [int]'在此函數中:2D數組中數組下標的無效類型'int [int]'
void Image::read(int* arr)
{
//Nested loop that reads the file
for(int i = 0; i < height; i++)
{
for(int k = 0; k < width; k++)
{
inputFile >> arr[i][k]; //Here's where I get the error
}
}
}
這裏是我的主要功能:
int main()
{
Image test("colorado1.dat"); //File with the integers
test.setWidth(500);
test.setHeight(500);
int array[test.getHeight()][test.getWidth()];
test.read(array);
//Loop to test if the function worked
for(int i = 0; i < 500; i++)
{
for(int k = 0; k < 500; k++)
{
cout << array[i][k] << " ";
}
}
}
你究竟對錯誤不瞭解? 'arr'是一個'int *',因此'arr [i]'是一個'int','arr [i] [k]'是一個明顯的錯誤,就像'int x = 4; int y = x [2];'也是。你在這裏有什麼不明白的地方?你也會在你的'main()'中得到另外一個編譯錯誤,這可能會讓你指向一個很大的喇叭線索。此外,你的'main()'聲明瞭一個變長數組,它是一個非標準的編譯器擴展,並且不是有效的C++。 –