当我尝试将数组的指针(其中包含程序中某些函数所需的参数)传递给一个结构时,我遇到了一个问题,然后应该将其传递给这些函数。例如,GSL 希望我以这种方式传递参数。
一个小示例程序如下所示:
#include <iostream>
using namespace std;
struct myparams
{
double * a;
double ** b;
};
int main()
{
double c[10] = {0,1,2,3,4,5,6,7,8,9};
double d[4][3] = {{1,2,3},{4,5,6},{7,8,9},{10,11,12}};
double** e = new double*[4];
for (int i = 0; i < 4; i++) {
e[i] = new double[3];
}
myparams params;
// THIS WORKS:
params.a = c;
for (int i = 0; i < 10; i++) {
cout << params.a[i] << endl;
}
// THIS DOESN'T WORK
params.b = d;
// THIS WORKS:
params.b = e;
delete[] e;
}
有什么问题
params.b = d
编译器抱怨"cannot convert 'double[4][3]' to 'double**' in assignment"
或类似的东西(从德语翻译)。