0

This is for a project I am doing for my college class and I could not find an answer for this that worked for me anywhere. Its very likely I did not understand the answers though.

I have a struct (menuItem), and that struct is put into a class (Menu). I have successfully created an array of struct menuItem inside class Menu before, but when I try to create a vector of menuItem inside Menu I get this error when I try to initialize its size

error C2059: syntax error : 'constant'

This is the offending code:

#include <vector>

using namespace std;

//previous array value
const int MAXCOUNT = 20;

struct menuItem
{
    void(*func)();
    char description[50];
};

class Menu
{
private:
    std::vector<menuItem> mi(20); // <<< "The error occurs when I set the vector to a size of 20"
    int count;
    void runSelection();
};

This error does not pup up when I initialize the vector as sizeless, but it pops up once again the second I use mi.resize(20); in the form of this contextual error: "Error, this deceleration has no storage class or type specifier", which makes no sense because I set the type of the vector mi as type menuItem per how I believe vectors are initialized.

I am assuming I am initializing the vector wrong somehow, or I am setting up the struct wrong. I've found "answers" stating that structs are classes without a private section which I knew, and that you have to have a constructor to initialize a vector of structs, which I did not know. Problem is, no constructor I've come up with has made the error go away. I am completely lost and would appreciate some assistance.

6
  • 1
    making the code as code sample should be better to read. Commented Oct 27, 2014 at 0:41
  • Seems like you put mi.resize(20); outside of a function. Commented Oct 27, 2014 at 0:42
  • This is the code as I want it to be, without using a mi.resize(20). I just wanted to state that doing mi.resize(20) did not seem to fix the issue either. Thank you btw for the advice raison, First time posting here did not realize that existed. Commented Oct 27, 2014 at 0:43
  • 1
    "I've found 'answers' stating that structs are classes without a private section which I knew" Nope, incorrect. Commented Oct 27, 2014 at 0:43
  • I see, I guess that was just another thing that I took incorrectly sigh Commented Oct 27, 2014 at 0:45

3 Answers 3

1

You can do this in C++11 like so

class Menu
{
private:
    std::vector<menuItem> mi = std::vector<menuItem>(20);
};

or if you are stuck using C++03 you will need to initialize it in the constructors initializer list

class Menu
{
public:
    Menu() : mi(20) {}

private:
    std::vector<menuItem> mi;
};
Sign up to request clarification or add additional context in comments.

Comments

1

For class members, you do not initialise them where you declare them (as the list of members is just that: a list of types and names), but instead in the constructor's member initialisation list.

Like this:

class Menu
{
    std::vector<menuItem> mi;    // no initialiser here

    Menu() : mi(20) {};          // but in the ctor-initializer instead
};

12 Comments

Not as inline as you'd like. {20} will use the list constructor and (20) isn't allowed in an NSDMI.
@chris Yeah it becomes a pain. That's why I didn't want to go into it here. Though until your comment I forgot that it's even harder than I remember! In fact, I shall just remove that addendum accordingly :)
Yeah, it's a shame that's really only that bad for types with list constructors. And then of course auto mi = std::vector<menuItem>(20); doesn't work because auto isn't allowed in NSDMIs either.
omg, do you mean my issue is im trying to setup how many vectors of my struct exist inside the initialization of the class in the .h, when I should be doing it in a constructor inside the .ccp. I should have known that...
@Stephen: Your terminology is off, but... yeah, more or less.
|
0

You're trying to pass parameters to a constructor of a non-static class member as part of the declaration, which is wrong.

Basically, you'll have to declare the data member in the declaration like this:

std::vector<menuitem> mi;

and as part of the constructor, call the appropriate resize, if that is even necessary.

Unless you know you need a minimum vector size of 20, I would forget about pre-sizing it and just let it grow as necessary.

Comments

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.