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