Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
125 views
in Technique[技术] by (71.8m points)

c++ - Arduino compile error while using reference to a struct in another struct

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 };

It ends up with the following error:

 sketch_feb08a.ino:24:53: error: cannot convert 'Select_Item* (*)[2]' to 'Select_Item*' in initialization

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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'
", menu_list[0]->item_name);
    printf("    %d - %s
", menu_list[0]->list[0].item_value, menu_list[0]->list[0].item_name);
    printf("    %d - %s
", menu_list[0]->list[1].item_value, menu_list[0]->list[1].item_name);
    printf("Menu item '%s'
", menu_list[1]->item_name);
    printf("    %d - %s
", menu_list[1]->list[0].item_value, menu_list[1]->list[0].item_name);
    printf("    %d - %s
", menu_list[1]->list[1].item_value, menu_list[1]->list[1].item_name);
    return 0;
}

Note the syntax for the initialization of menu_item1 and menu_item2.

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...