2016-02-22 195 views
1

我試圖將二維數組輸入到函數中。我不知道這個數組的行數或列數,它通過CImg加載到C++中。這是我有:在C++中將未知大小的二維數組傳遞到函數中

// Main function: 
int main() 
{ 
    int rows, columns; 
    float summation; 
    CImg<unsigned char> prettypicture("prettypicture.pgm"); 
    rows = prettypicture.height(); 
    columns = prettypicture.width(); 

    summation = SUM(prettypicture[][], rows, columns); 
} 

// Summation function: 
float SUM(int **picture, int rows, int column) 
{ 
... // there is some code here but I don think this is important. 
} 

我想數組傳遞到求和函數和我知道,我應該以某種方式使用指針,但我不知道如何做到這一點。任何幫助將非常感激。

謝謝

(對不起,是一個小白)

回答

1

試試這個:

summation = SUM(prettypicture.data(), rows, columns); 

,讓您的SUM函數是這樣的:

float SUM(char* picture, int rows, int column) ... 

您需要data通過(如果你想有一個指針數據),因爲這是CImg提供的。這是一個指向角色的指針,因爲那是你所擁有的CImg;這是一個char*,而不是char**,因爲這是數據提供的。

您沒有向我們展示SUM函數的內部,所以我想知道您是否可以很好地傳入CImg而不是其數據,並調用需要位置的成員函數atXY。很難說沒有看到更多。

有關012mg和CImg的其他成員函數的更多信息,請參見http://cimg.eu/reference/structcimg__library_1_1CImg.html

+0

非常感謝,正是我所需要的:D – ProNoobSry

0

,爲什麼不把它作爲參考?

summation = SUM(prettypicture); 

float SUM(const CImg<unsinged char>& picture) { 
    // ... 
}