c++ - 在另一个结构中使用对结构的引用时Arduino编译错误

标签 c++ pointers struct compiler-errors arduino

我正在为一个小型arduino项目编写一个16x2 LCD菜单。
我快完成了,但是我不明白最后一个小问题。

下面的(简化的)代码会产生此问题:

int var1=0;
int var2=0;

typedef struct {
    unsigned int item_value;
    char item_name[17];
} Select_Item;

typedef struct {
  char item_name[17];
  int* variable;
  Select_Item* list;
} Menu_Item;

Select_Item sel_one = { 0, "Selection 1" };
Select_Item sel_two = { 1, "Selection 2" };
Select_Item* sel_list[2] = { &sel_one, &sel_two };

Menu_Item menu_item1 = { "Item 1", &var1, NULL }; 
Menu_Item menu_item2 = { "Item 2", &var2, &sel_list }; 
Menu_Item* menu_list[2] = { &menu_item1, &menu_item2 };

最终出现以下错误:
 sketch_feb08a.ino:24:53: error: cannot convert 'Select_Item* (*)[2]' to 'Select_Item*' in initialization

在代码中,我正在访问变量的值并将其显示在显示屏中,一旦编辑,我就可以将其写回到变量中。只要我只有要显示/编辑的数字,那不是问题。
现在,为了易于使用,我想添加某种选项菜单,用户可以从中进行选择。应该显示item_name,而不是原始值,但当然应该在场景后面使用item_value。
这就是为什么我引入了Select_Item结构。

我不明白该错误信息。怎么了

最佳答案

我认为您正在尝试执行以下操作:

int var1=0;
int var2=0;

typedef struct {
    unsigned int item_value;
    char item_name[17];
} Select_Item;

typedef struct {
  char item_name[17];
  int* variable;
  Select_Item* list;
} Menu_Item;

Select_Item sel_one = { 0, "Selection 1" };
Select_Item sel_two = { 1, "Selection 2" };
Select_Item sel_three = {2, "Selection 3" };
Select_Item sel_four = {3, "Selection 4" };

Select_Item* sel_list_1[] = { &sel_one, &sel_two };
Select_Item* sel_list_2[] = { &sel_three, &sel_four };

Menu_Item menu_item1 = { "Item 1", &var1, sel_list_1[0] }; // Note the syntax here
Menu_Item menu_item2 = { "Item 2", &var2, sel_list_2[0] }; // Note the syntax here
Menu_Item* menu_list[2] = { &menu_item1, &menu_item2 };

// Added for testing
int main()
{
    printf("Menu item '%s'\n", menu_list[0]->item_name);
    printf("    %d - %s\n", menu_list[0]->list[0].item_value, menu_list[0]->list[0].item_name);
    printf("    %d - %s\n", menu_list[0]->list[1].item_value, menu_list[0]->list[1].item_name);
    printf("Menu item '%s'\n", menu_list[1]->item_name);
    printf("    %d - %s\n", menu_list[1]->list[0].item_value, menu_list[1]->list[0].item_name);
    printf("    %d - %s\n", menu_list[1]->list[1].item_value, menu_list[1]->list[1].item_name);
    return 0;
}

注意menu_item1menu_item2初始化的语法。

我添加了main()只是为了我可以使用普通的C编译器进行测试(因为我没有配备LCD的Arduino)。我的测试输出如下:
Menu Item 'Item 1'
    0 - Selection 1
    1 - Selection 2
Menu Item 'Item 2'
    3 - Selection 3
    4 - Selection 4

我认为这就是您要使用数据结构实现的目标。您只需要使它适应您的Arduino代码,并使用这些数据结构来管理LCD上的菜单选择即可。

关于c++ - 在另一个结构中使用对结构的引用时Arduino编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28397551/

相关文章:

c - 打印结构体的值不显示正确的值

c++ - 与 std::list 一起使用的指针上的接口(interface)类型转换的多重继承

c++ - 使用 enable_if 和 is_integral 来制作分布特征

创建一个用户空间指针来保存回文并将该指针传递给 copy_from_user(void *to, const void __user *mypointer, unsigned long count)

c - 如何检查字符串C中的空格?

c - 使用 sizeof 分配和初始化矩阵

c - 在 C 中使用结构体作为输入的语法

c++ - C++ 中的 11db 错误; Xcode。

c++ - 从 XML 中删除了不应该删除的多个空格

struct - Go 中的自动类型断言