将2-D数组传递给double function的错误

时间:2012-03-12 00:15:32

标签: c++ multidimensional-array

我正在研究一个程序,它将文件中的高度值读入一个二维数组,一个矩阵,我试图将该数组传递给另一个找到最大值的函数。我理解,默认情况下,数组是通过引用传递的,但我并不是试图更改函数中数组的值,所以这应该不重要。我已经浏览了几个关于调用数组的页面,但是在编译代码时我无法找到任何关于错误类型的提及。问题似乎在于被调用的参数的数量或被调用的方式,但我看不出函数的各种外观有任何差异。我的猜测是有一些关于传递二维阵列的事情,我在课堂上没有被告知,而且我还没有自己学习。任何帮助将不胜感激。 代码是:

#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]'for argument' 1'到'double find_max(double(*)[3600],double,double,int,int)'

2 个答案:

答案 0 :(得分:1)

从我在代码中看到的,有一些小故障。

  • 您已将数组高程定义为

    双高程[lat_row] [lon_col];

这不会起作用,因为c样式数组的大小必须在编译期间可以确定。由于lat_row和lon_col是变量,这是一个错误。

因此,您可以使用具有动态内存分配的数组,也可以使用std :: vector,这在大多数情况下更可取。所以,在你的情况下你可以有类似的东西:

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

然后只使用该数组或double数组。 然后,您的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 :: resize),或者只是分配足够的空间(std :: vector :: reserve)和然后初始化它。如果它很大,那可能会提高性能。

但是,如果你选择使用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。它更容易使用,至少如果您的经验有限。它还会处理内存分配/释放,这会导致许多不好的事情,溢出,泄漏等,如果你使用向量,这将被避免。

答案 1 :(得分:0)

您正在尝试传递一个动态确定大小的数组(即在运行时),并将其传递给一个函数,该函数要求数组在编译时确定其第二个维度为3600(这看起来很漂亮合理的抱怨,实际上。)