Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize an Array of Nested structures

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

like image 566
Dustin Burns Avatar asked Nov 19 '25 22:11

Dustin Burns


1 Answers

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} },
}
like image 93
Jonathan Wood Avatar answered Nov 22 '25 11:11

Jonathan Wood



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!