【发布时间】:2012-03-12 00:15:32
【问题描述】:
我正在开发一个程序,该程序将高度值从文件读取到二维数组、矩阵中,并且我试图将该数组传递给另一个查找最大值的函数。我知道,默认情况下,数组是通过引用传递的,但我并不想在函数中更改数组的值,所以这无关紧要。我已经浏览了几页关于调用数组的页面,但是我无法找到任何提及我在编译代码时遇到的错误类型。问题似乎在于被调用的参数数量或被调用的方式,但我看不出函数的各种外观有任何差异。我的猜测是传递一个二维数组有一些东西,我在课堂上没有被告知,而且我还没有自己学过。任何帮助将不胜感激。 代码是:
#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;
}
但是,当我调用该函数时,我收到以下错误:
错误:无法将参数 'double (*)[(((long unsigned int)(((long int)lon_col) - 1)) + 1u)]' 转换为 'double (*)[3600]' 1' to 'double find_max(double (*)[3600], double, double, int, int)'
【问题讨论】:
标签: c++ multidimensional-array