I have a struct that I have defined to represent a menu. It stores the text for the menu, shortcut, callback for when the menu is selected and any sub menus. This is all working quite well but the code to create the menu is a little cumbersome and it makes it difficult to modify the menu and to visualise what the menu will look like. It was taking 4 to 6 lines to create each menu items and the submenus were not underneath the menu that were created under. So I created some constructors for the struct and could call those to reduce the code. The problem I ran into was passing in submenus
eg, code below shows me trying to create the top menu with 2 submenus. Everything works except I cannot pass the anonymous array of MenuItem to the constructor. Constructors are not shown for brevity. If this is not possible, any other way to write so that I can code it similar to the layout of the menu? Thanks.
struct MenuItem {
char* menuName = NULL;
char* keyText = NULL;
MenuItem* subMenus = NULL;
int subMenuCount = 0;
void (*callback)(void) = 0;
};
MenuItem* topMenu;
topMenu = new MenuItem("Top Menu", "1", 2, //2 is count of submenus
{ new MenuItem("Configure Device", "1"),
new MenuItem("Configure WiFi", "2")
});
MikeKulls:
Constructors are not shown for brevity.
Brevity is the goal. Got it.
MikeKulls:
Everything works except I cannot pass the anonymous array of MenuItem to the constructor.
Huh?
topMenu is a pointer. You are trying to initialize it with a list of pointers.
To break out the part that is not working, assume a function that takes an array of my struct
void doSomething(MenuItem submenus[]) {
}
Then to call this with an anonymous array:
doSomething( { new MenuItem("Config Wifi"), new MenuItem("Config Settings") } )
It gives an error trying to pass the anonymous array into the function.
This is about the simplest code I can produce to demonstrate the error. How do I eliminate the error?
struct MenuItem {
MenuItem(char* newText) {
text = newText;
}
char* text;
};
void doSomething(MenuItem subMenus[]) {
}
void setup() {
doSomething( { new MenuItem("config wifi"), new MenuItem("config other") } );
}
It gives an error trying to pass the anonymous array into the function.
No, it does not. The compiler tells you EXACTLY what the problem is. It does not do some hand-waving, saying "You can't do that".
How do I eliminate the error?
With proper coding, of course. Without seeing the EXACT error you are trying to eliminate, you can't expect us to help you eliminate it.
Untested:
topMenu = new MenuItem("Top Menu", "1",
new MenuItem[2] { MenuItem("Configure Device", "1"), MenuItem("Configure WiFi", "2") },
2 /* 2 is count of submenus */);
arduarn:
Untested:
topMenu = new MenuItem("Top Menu", "1",
new MenuItem[2] { MenuItem("Configure Device", "1"), MenuItem("Configure WiFi", "2") },
2 /* 2 is count of submenus */);
Great work! I really did my due diligence on this and google had various different answers that didn't work. That is awesome, it will really simplify my code and making it much clearer. Thank you.
MikeKulls:
Great work!
Well, except for the fact that in the "right" place that code crawls through the heap until there is nothing left then causes your program to crash in a nearly impossible way to diagnose.
As for the heap, I don't think it will be a big issue. These structs will be created once at the start and never changed.