Go Down

Topic: Anonymous Array in Constructor (Read 317 times) previous topic - next topic

MikeKulls

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.

Code: [Select]


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



Coding Badly

Constructors are not shown for brevity.
Brevity is the goal.  Got it.

Everything works except I cannot pass the anonymous array of MenuItem to the constructor.
Huh?


arduino_new

topMenu is a pointer. You are trying to initialize it with a list of pointers.

MikeKulls

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.

MikeKulls

This is about the simplest code I can produce to demonstrate the error. How do I eliminate the error?

Code: [Select]

struct MenuItem {
  MenuItem(char* newText) {
    text = newText;
  }
  char* text;
};

void doSomething(MenuItem subMenus[]) {
 
}

void setup() {
  doSomething( { new MenuItem("config wifi"), new MenuItem("config other") } );
}

PaulS

Quote
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".

Quote
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.
The art of getting good answers lies in asking good questions.

arduarn

Untested:
Code: [Select]
topMenu = new MenuItem("Top Menu", "1",
  new MenuItem[2]  { MenuItem("Configure Device", "1"), MenuItem("Configure WiFi", "2") },
  2 /* 2 is count of submenus */);

MikeKulls

Untested:
Code: [Select]
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.

Coding Badly

#8
May 21, 2019, 07:33 pm Last Edit: May 24, 2019, 06:39 am by Coding Badly
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.


MikeKulls

#9
May 22, 2019, 12:16 am Last Edit: May 24, 2019, 06:40 am by Coding Badly
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.

Go Up