我的指向结构的指针数组有什么问题?
What's wrong with my array of pointers to arrays of pointers to structs?
一个简短的解释,我想做什么:我想构建一个由structs表示的数据元素的层次树。这些元素应该是双重链接的,这样我就可以走路了。我不想使用动态分配,因为树的结构在运行时不会改变。
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
在将变量和数组的初始化移动到函数代码后,我有一个新的错误消息;更有意义的是:
/[path_truncated]/initialisation.cpp: In function 'void shit()':
/[path_truncated]/initialisation.cpp:46: error: cannot convert 'menu* (*)[2]' to 'menu* (*)[]' in assignment
将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;
相关文章:
- 警告处理为错误这里有什么问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 当我尝试添加 2 个大字符串时,我无法弄清楚出了什么问题
- 违反const正确性:我应该现实地期待什么问题
- 这个带有模板<类 Vector 的C++代码片段有什么问题>
- 我的逻辑反转字符串中的元音有什么问题?
- 需要以下代码的帮助,下面的代码有什么问题
- 常量公共成员有什么问题?
- 以下代码中的函数模板有什么问题?
- 这个返回元素位置的基于循环的函数有什么问题?
- creat_list2功能有什么问题?
- 格式说明符C++有什么问题
- 任何人都可以告诉我我的 C++ 代码出了什么问题?
- 从 argv[1] 转换为字符 * 字符串后有什么问题?
- 我的堆栈和库存清单程序的结构有什么问题?
- 此工厂功能有什么问题?
- 以下 C++ 代码有什么问题?
- 数组为此合并排序函数提供了正确的输出,但向量给出了不正确的输出.出了什么问题?
- reinterpret_cast,只读访问,简单的可复制类型,会出什么问题?
- 它解决了什么问题,对于非真空初始化,生命周期在初始化之前就开始了