0

I'm new to cpp since a few days and struggling with the following definition:

struct menuItem {
    char* name;
    int value;
};

struct topMenu {
    int menuIcon;
    char* Name;
    menuItem item[];
};

topMenu menuRoot[] = {
    { 0, "File", 
        {"Open ...", 1},
        {"New ...", 4},
        {"Close", 1},
        {"Exit", 3}

    },
    { 0, "Edit", 
        {"Cut ", 3},
        {"Copy", 8},
        {"Paste", 2},
        {"Find", 1},
        {"Replace", 6}

    },
    { 0, "Help", 
        {"Help", 7},
        {"About", 9},
        {"Update ..", 9}
    }  
};

I receive the error

33:1: error: too many initializers for 'menuItem [0]'
33:1: error: too many initializers for 'topMenu'
33:1: error: too many initializers for 'menuItem [0]'
33:1: error: too many initializers for 'topMenu'
33:1: error: too many initializers for 'menuItem [0]'
33:1: error: too many initializers for 'topMenu'

Sorry, i'm a little bit stuck ... maybe its just to late :) Thanks in advance.

6
  • 1
    [Off topic] Do not use a char* to point to string literals. It was deprecated in C++03 and removed in C++11. Either use a const char* or a std::string/std::string_view Commented Jul 10, 2019 at 21:33
  • 2
    You need to wrap each menuItem array in an additional { ({{"Help", 7}, {"About", 9}, {"Update ..", 9}}). Commented Jul 10, 2019 at 21:35
  • 3
    What do you expect from menuItem item[];? should it be std::vector instead? Commented Jul 10, 2019 at 21:41
  • 2
    Remember that size of arrays in C++ has to be determined at compile time. Commented Jul 10, 2019 at 21:52
  • 2
    menuItem item[]; is not allowed in Standard C++, you must specify a size (or use a different construct altogether) Commented Jul 10, 2019 at 22:57

1 Answer 1

2

First, wrap the menuItem item[] array in {} because currently it thinks there are many arrays rather than just one.

Next, change the char * to const char * as was mentioned.

Now for the flexible amount of menuItem structs, use a vector rather than an array.

#include <vector> 

struct menuItem {
    const char *name;
    int value;
};

struct topMenu {
    int menuIcon;
    const char *Name;
    std::vector<menuItem> item;
};

topMenu menuRoot[] = {
    { 0, "File", {
        {"Open ...", 1},
        {"Open ...", 1},
        {"Open ...", 1},
        {"Open ...", 1},
    }
    },
    { 0, "Help", {
        {"Open ...", 1},
        {"Open ...", 1},
        {"Open ...", 1}, }
    }
};
Sign up to request clarification or add additional context in comments.

2 Comments

My bad, just laziness on my part.
The menu is basically static (won't be changed during runtime ( - yet)). Therefore i could set all sizes. But i just thought there was another way. Since it will be use on an embedded system, i've care about memory. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.