Stephen
Stephen

Reputation: 25

Error creating a size for a vector of Structs C++

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.

Upvotes: 0

Views: 789

Answers (3)

Timo Geusch
Timo Geusch

Reputation: 24341

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.

Upvotes: 0

Captain Obvlious
Captain Obvlious

Reputation: 20063

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

Upvotes: 1

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

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

Upvotes: 1

Related Questions