我正在尝试使用多维数组,但出现错误,无论我尝试何种变体,似乎都无法使其正常工作,我也无法在在线文档中找到任何帮助...
const int SIZE = 50;
double a(double A[][SIZE], int m, int n);
void main(){
double arg[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
cout << a(arg[SIZE], 3, 3);//I get an error here
}
为什么不 cout << a(arg, 3, 3);
工作吗? arg
函数中是指针,为什么还需要指定大小或其他内容?
我得到的错误是:
Error 1 error C2664: 'double MeanMatrix(double [][50],int,int)' : cannot convert argument 1 from 'double' to 'double [][50]'
最佳答案
可以按函数规定的一维传入:
double foo(double A[][3], int m);
foo(arg, 3);
或者您可以通过模板推导出两个维度:
template <size_t M, size_t N>
double foo(double (&A)[M][N]);
foo(arg);
但您的函数 a
现在要求其中一个维度为 SIZE
,arg
不满足,无论您重新传入 arg[SIZE]
,这是未定义的行为,因为您正在索引数组的末尾并且另外是一个 double[3]
,这不会匹配类型。
关于c++ - 使用仅指定一维的多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27584839/