2014-10-10 98 views
0

一个简短的解释,我想要做什么:我想要构建由结构表示的数据元素的分层树。这些元素应该双重链接,这样我才能走路。我不想使用动态分配,因为树的结构在运行时不会改变。指向指向结构体的指针数组的指针数组有什么问题?

struct menu { 
    uint8_t type; // typ des Menüpunkts 
    struct menu * parent; // Pointer auf den übergeordneten MP 
    struct menu *(*children)[]; // Pointer auf untergeordnete MP 
    }; 

struct menu *(*menll[5])[]; // auxillary array 

struct menu gl_menlist[5]= 
{ 
    {0,0,menll[0]}, 
    {0,0,menll[1]}, 
    {0,0,menll[2]}, 
    {0,0,menll[3]}, 
    {0,0,menll[4]} 
}; 

struct menu * rxakvt01[]= {&gl_menlist[3], &gl_menlist[4]}; 

menll[0]=&rxakvt01; 

代码在最后一行失败,此错误消息:

In file included from Dis0_10.ino:6: var/folders/jl/nv1qvh6n569cxq9xxfd6dx980000gn/T/build753942546562757431.tmp/initialisation.h:71: error: expected constructor, destructor, or type conversion before '=' token 

后移动VARS和阵列的initialisations到一个功能码,我有一个新的错误消息;有意义得多,但:

/[path_truncated]/initialisation.cpp: In function 'void shit()': 
/[path_truncated]/initialisation.cpp:46: error: cannot convert 'menu* (*)[2]' to 'menu* (*)[]' in assignment 
+0

这是我见过的第一个C编译器错误引用构造函数和析构函数。当然,这不是C++? – abligh 2014-10-10 18:30:57

+0

那是什么:'struct menu *(* menll [5])[];'? – 2501 2014-10-10 18:34:54

+0

它是一个C++编译器。不幸的是我无法改变它。最后一次我问了一个类似的问题,我用C++标记了它,然后我在C++标签下寻找c解决方案。 – Ariser 2014-10-10 18:35:25

回答

0

可能的是,转换menu *(*)[2]menu *(*)[]失败是编译器的缺点。

如果你的子菜单的大小不一定是动态的,然后简单地声明

struct menu *(*children)[2]; 

struct menu *(*menll[5])[2]; 

,你应该罚款。

如果您需要动态菜单大小,您需要知道子菜单在某个时间点的长度,这是编译器无法为您推导的,因此建议使用某种类型的标记以指示一个子菜单。

我发现另一个可能不是你在这里介绍的东西。 gl_menlist的定义在定义时包含menll的值,稍后的分配不会改变该值。这是什么应该工作:

struct menu { 
    uint8_t type; // typ des Menüpunkts 
    struct menu * parent; // Pointer auf den übergeordneten MP 
    struct menu ***children; // Pointer auf untergeordnete MP 
}; 

struct menu *(*menll[5]); 

struct menu gl_menlist[5] = 
{ 
    { 0, 0, &menll[0] }, 
    { 0, 0, &menll[1] }, 
    { 0, 0, &menll[2] }, 
    { 0, 0, &menll[3] }, 
    { 0, 0, &menll[4] } 
}; 

struct menu *rxakvt01[] = { &gl_menlist[2], 0 }; 
struct menu *rxakvt02[] = { &gl_menlist[3], &gl_menlist[4], 0 }; 

menll[0] = rxakvt01; 
menll[1] = rxakvt02; 
+0

由于子菜单的数量因菜单而异,因此我需要动态菜单大小。我做了一个类型转换:'menll [0] =(menu *(*)[])&rxakvt01;'它似乎工作(我还有其他错误需要先解决),但这不是一个好的编程习惯,不是它?你发现的第二件事是一个真正的表演塞。谢谢。 – Ariser 2014-10-10 21:56:56