2012-03-12 79 views
0

我正在研究一個程序,該程序將文件中的高度值讀入到二維數組,矩陣中,並且我試圖將該數組傳遞給另一個函數最大值。我明白,默認情況下,數組是通過引用傳遞的,但我並不試圖在函數中更改數組的值,所以這應該不重要。我已經瀏覽了幾個關於調用數組的頁面,但是在編譯代碼時我一直沒有找到任何提及的錯誤類型。問題似乎在於調用的參數數量或調用的方式,但我不能在函數的各種外觀中看到任何差異。我的猜測是關於傳遞一個二維數組,我沒有在課堂上講過,而且我還沒有學過。任何幫助將不勝感激。 的代碼是:將二維數組傳遞給double function的錯誤

#include <fstream> 
#include <iostream> 
#include <string> 
#include <cstdlib> 

using namespace std; 

// First instance of function declaration 
double find_max(double elevations[][3600], double ilat, double ilon, int nlat, int nlon); 

int main(int argc, char *argv[]) { 

// Declare program variables 
double lat_init, lon_init; 
double lat_res, lon_res; 
double peak, valley; 
int lon_col, lat_row; 
string indat, inpoints; 
. 
. 
. 
double elevations[lat_row][lon_col]; 

// Open and read topographic data file 
ifstream topo_points; 
topo_points.open(inpoints.c_str()); 

for (int i=0; i<lat_row; i++) { 
    for (int j=0; j<lon_col; j++) 
     topo_points >> elevations[i][j]; 
} 

// Call function to find peak in the data 
peak = find_max(elevations, lat_init, lon_init, lat_row, lon_col); 

return 0; 

} 


// ***** Here lie the functions ***** 

// This function reads in the array of elevations, initial latitude and longitude 
// of the data, and the number of data points and uses this information to find 
// the latidude and longitude of the highest point on earth 
double find_max(double elev[][3600], double ilat, double ilon, int nlat, int nlon) { 

double num, max; 
double latpos, lonpos; 

max = 0; 

for (int i=0; i<nlat; i++) { 
    for (int j=0; j<nlon; j++) { 
    num = elev[i][j]; 
    if (num > max) { 
     max=num; 
     latpos= ilat - i; 
     lonpos= ilon + j; 
    } 
    } 
} 

cout << "The tallest peak on earth has an altitude of " << max; 
cout << " and is located at " << latpos << "deg latitude and "; 
cout << lonpos << "deg longitude"; 

return max; 
} 

然而,當我打電話,我得到以下錯誤的函數:

錯誤:無法轉換「雙(*)(((長無符號整數)(((長int)lon_col) - 1))+ 1u)]''將double(*)[3600]'作爲參數'1'變爲'double find_max(double(*)[3600],double,double,int,int)'

+0

確實lon_col = 3600? – tmpearce 2012-03-12 01:03:30

回答

0

您試圖傳遞一個數組,其大小是動態確定的(即在運行時),並將其傳遞給一個函數,該函數期望該數組的第二維在編譯時確定爲3600(這看起來像非常合理的事情實際上抱怨)。

1

從我在代碼中看到的,有幾個小故障。

  • 您已定義的陣列隆起作爲

    雙凸起[lat_row] [lon_col];

這是不會工作的,因爲c樣式數組的大小在編譯期間必須確定。由於lat_row和lon_col是變量,這是一個錯誤。

所以,你可以使用具有動態內存分配的數組,或者std :: vector,這在大多數情況下是可取的。所以,在你的情況下,你可以有這樣的東西:

typedef std::vector< std::vector<double> > ElevationsType; 
ElevationsType elevations; 

然後只是使用該數組或雙數組。 然後,你find_max函數可以聲明爲:

double find_max(const ElevationsType &elevations, double ilat, double ilon); 

注意,在這種情況下,你不需要NLAT和nlon通過,因爲你可能只是做:

ElevationsType::size_type nlat, nlon, i, j; 
nlat = elevations.size(); 

for (i = 0; i != nlat; ++i) { 
    nlon = elevations[i].size(); 
    for (j = 0; j != nlon; ++j) { 
     const double element = elevations[i][j]; 
     // do whatever you need to do with the element 
    } 
} 

當然,如果你的數組有固定的大小,你可以在創建類型ElevationsType的對象或者只是分配足夠的空間(std :: vector :: reserve)然後初始化它的時候設置它(std :: vector :: resize)。如果它很大,那可能會提高性能。

但是,如果你選擇去與C風格的數組,這將是這樣的:

double **elevations = (double **)malloc(sizeof(double*) * lat_row); 
for (size_t i = 0; i != lat_row; ++i) { 
    elevations[i] = (double*)malloc(sizeof(double) * lat_col); 

    // initialize the elements 
    for (size_t j = 0; j != lat_col; ++j) { 
     elevations[i][j] = 100.0; /* your value */ 
     std::cout << "elevations[" << i << "][" << j << "] = " << elevations[i][j] << std::endl; 
    } 
} 

,這是許多人更繁瑣的..可以這麼說。如果你走這條路,就不要忘記用free()釋放所有分配的內存。

您也可以使用C++ new運算符來分配內存,但原理幾乎相同。

所以我建議你使用std :: vector。至少如果您的經驗有限,與其合作更容易。它還將處理內存分配/釋放,並導致許多不好的事情,溢出,泄漏等,如果使用矢量,將會避免這種情況。

+0

非常感謝您的回答!我發現只要使用整數值而不是程序變量設置數組大小,那麼調用函數就可以正常工作。我對C++還是有點新鮮感,儘管過去我嘗試過使用vector,但我並不完全熟悉它們。這似乎是一個變好的好機會! – user1263011 2012-03-12 03:26:59

+0

是的,堅持使用「std :: vector」來避免使用C風格數組(指針等)的困難。儘管如此(C風格的數組)還是有用的,如果你有時間的話,那裏有很多關於這個主題的文章。看到我在這個論壇上的兩個最新帖子的初學者:http://stackoverflow.com/questions/9583086/2dimensional-array-pointer-manipulation-in-c/9608139#9608139和http://stackoverflow.com/questions/9672731 /操縱-多維陣列與 - 功能 - 在-C/9677552#9677552 – Larry 2012-03-13 14:57:55