在另一个结构中使用对结构的引用时 Arduino 编译错误 [英] Arduino compile error while using reference to a struct in another struct

查看:19
本文介绍了在另一个结构中使用对结构的引用时 Arduino 编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm on prgramming a menu for an 16x2 LCD for a small arduino project. I'm nearly finished, but i do not understand the last small issue.

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

The following (simplified) code generates the problem:

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 结构的原因.

In the code i'm accessing the values from the variables and show it in the display and once edited i can write it back to the variable. That was not the problem as long as i had just numbers to show/edit. Now for ease of use i wanted to add some kind of option menu, where the user can choose from the options. The item_name should be displayed, instead of the raw value, but of course the item_value should be used behind the scene. That is why i introduced the Select_Item struct.

我不明白错误信息.这里有什么问题吗?

I don't understand the error message. What's wrong here?

推荐答案

我认为你想要做的是这样的:

I think what you're trying to do is something like this:

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 的初始化语法.

Note the syntax for the initialization of menu_item1 and menu_item2.

我添加了 main() 只是为了我可以用一个普通的 C 编译器测试它(因为我没有一个带 LCD 的 Arduino 方便).我的测试输出如下所示:

I added main() just so I could test this with a normal C compiler (since I don't have an Arduino with an LCD handy). The output from my testing looks like this:

Menu Item 'Item 1'
    0 - Selection 1
    1 - Selection 2
Menu Item 'Item 2'
    3 - Selection 3
    4 - Selection 4

而且我认为这就是您试图通过数据结构实现的目标.您只需将其调整到您的 Arduino 代码并使用这些数据结构来管理 LCD 上的菜单选择.

And I think that's what you're trying to achieve with your data structures. You'll just need to adapt this to your Arduino code and use these data structures to manage the menu selections on your LCD.

这篇关于在另一个结构中使用对结构的引用时 Arduino 编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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