C ++函数调用到二维数组 [英] C++ function calling to 2d array

查看:208
本文介绍了C ++函数调用到二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我在类的构造函数County.h:

Hey guys I have in class County.h constructor:

struct Country {
    Country(double**, int);
};

和主我有图[尺寸] [SIZE] ,我想呼吁构造

and in main I have graph[Size][Size] and I want to call the constructor for County.

int main() {
    double graph[Size][Size];
    Country c(graph, 0);
}

但它给我的错误呼叫没有匹配函数'县县::(双[22] [22],INT)

我可以为了解决这个问题做什么?谢谢

What I can do in order to solve this problem? Thank you

推荐答案

双击[尺寸] [SIZE] 双** 根本不是同一类型。这就是你的编译器不喜欢。

double [Size][Size] and double** are not at all the same type. That is what your compiler doesn't like.

更改您的构造函数原型或声明阵列的方式。但你不能直接浇铸数组的数组指针的指针。

Change your constructor prototype or the way you declare your array. But you cannot directly cast an array of array to a pointer of pointer.

struct Country {
    Country(double[Size][Size], int);
};

int main() {
    double** graph = new (double*)[Size];
    for (int i = 0; i < Size; ++i) {
        graph[i] = new double[Size];
    }
    Country c(graph, 0);

    // Don't forget to delete your graph then.
}

注意,第一个要求,你会知道大小之前您code开始执行(存储尺寸比如在一个宏),但第二一个是更长的时间来code,你将不得不处理更多的RAM内存,如果你不小心,可导致错误。

Note that the first one requires that you would know the size before your code start its execution (storing Size in a macro for instance), but the second one is longer to code, and you will have to manipulate more RAM memory, that can lead to mistakes if you are not careful.

这篇关于C ++函数调用到二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆