3

I'm trying to write a program that sets up a nested structure and then initializes an array of that structure. It's giving me some weird errors. Here's all the relevant code:

//Structure called Stats for storing initial character stats
struct Stats{
    string name;
    int level;
    int HP;
    int STR;
    int CON;
    int DEX;
    int INT;
    int WIS;
    int CHA;};

//Structure called Growth for storing character growth per level.
struct Growth{
    int HPperlvl;
    int STRperlvl;
    int CONperlvl;
    int DEXperlvl;
    int INTperlvl;
    int WISperlvl;
    int CHAperlvl;};

struct Holdstats{
    Stats classstats;
    Growth classgrowth;};

const int SIZE = 10;

Holdstats classlist[SIZE];

Holdstats charlist[SIZE];

//Define initial classes, to be stored in the Classes structure
classlist[0].classstats = {"Fighter", 1, 18, 10, 10, 10, 10, 10, 10};
classlist[0].classgrowth = {1,1,1,1,1,1,1};

classlist[1].classstats = {"Wizard", 1, 10, 10, 10, 10, 10, 10};
classlist[1].classgrowth = {1,1,1,1,1,1,1}

My compiler thinks that when I type "classlist[0].classstats" that I'm trying to initialize an array of size 0. The way I read this I'm trying to access the first element of the classlist array. Is this written correctly?

It'd be great if someone could give me a short example of what such an array looks like. From there I'm thinking of writing it as a vector

2
  • Didn't even read the question yet, but you need a semicolon after the struct declaration. Commented Dec 10, 2012 at 19:58
  • Yeah I noticed that, bad copy paste error lol. Commented Dec 10, 2012 at 19:59

2 Answers 2

2

You didn't show what all your types are but you should be able to take this basic approach.

Holdstats classlist[SIZE] = {
    { {"Fighter", 1, 18, 10, 10, 10, 10, 10, 10}, {1,1,1,1,1,1,1} },
    { {"Wizard", 1, 10, 10, 10, 10, 10, 10}, {1,1,1,1,1,1,1} },
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I had no idea you could do it like that. This solves my problem but I'm still interested in why I was getting the error in the first place
0

your struct Holdstats holds two other structs of type classstats and classgrowth. Remember these are structs, not arrays, so im not completely sure why you assign them like so:

classlist[0].classstats = {"Fighter", 1, 18, 10, 10, 10, 10, 10, 10};

i would guess you want to fill out the stats struct inside the holdstats struct itself, which would be done below:

classlist[0].classstats.health = 15; //guessing you have a member named health
//OR if you create a constructor for you classstats with the proper copy constructor
classlist[0].classstats = classstats("Fighter", 1, 18, 10, 10, 10, 10, 10, 10);
//OR if you have an assign function
classlist[0].classstats.assign("Fighter", 1, 18, 10, 10, 10, 10, 10, 10);

1 Comment

It's still acting like when I type classlist[0].classstats that I'm trying to initialize an array of size 0 instead of accessing the first element of the array. Any idea how to fix this? Thanks very much though, I think I get why I have to do it that way

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.