gpt4 book ai didi

c++ - 使用仅指定一维的多维数组

转载 作者:行者123 更新时间:2023-11-28 00:20:54 25 4
gpt4 key购买 nike

我正在尝试使用多维数组,但出现错误,无论我尝试何种变体,似乎都无法使其正常工作,我也无法在在线文档中找到任何帮助...

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 现在要求其中一个维度为 SIZEarg 不满足,无论您重新传入 arg[SIZE],这是未定义的行为,因为您正在索引数组的末尾并且另外是一个 double[3],这不会匹配类型。

关于c++ - 使用仅指定一维的多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27584839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com