Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems initializing arrays in structs in arrays in structs

I've been tasked to remove some compiler warning. I've been able to boil the problem down to the following example, which I am scratching my head why it won't work. I guess I don't know how to initialize stuff in C++. Any help would be appreciated.

I use g++ like so: g++ init_arr.cpp

Here's the code. I want to initialize all the people at all the tables in Aisle pizza:

// init_arr.cpp
#include <iostream>
#include <string>
#include <sstream>

using namespace std;


struct Person {
    int    id;
    string name;
    double money;
};


struct Table {
    Person tab[4];
};


struct Aisle {
    Table ais[3];
};

int main() {
    cout << "main function()" << endl;

    Aisle pizza =
        {
            {  // Table 0
                { 0, "Tom", 100.0 },
                { 1, "Mary", 101.0 },
                { 2, "Jane", 103.0 },
                { 3, "Joe",  104.0 }
            },

            {  // Table 1
                { 0, "Tom", 100.0 },
                { 1, "Mary", 101.0 },
                { 2, "Jane", 103.0 },
                { 3, "Joe",  104.0 }
            },

            {  // Table 2
                { 0, "Tom", 100.0 },
                { 1, "Mary", 101.0 },
                { 2, "Jane", 103.0 },
                { 3, "Joe",  104.0 }
            }
        };

    return 0;
}

I thought the above would work, but I get the following error:

g++ init_arr.cpp -std=gnu++0x
init_arr.cpp: In function ‘int main()’:
init_arr.cpp:49: error: too many initializers for ‘Table [3]’
init_arr.cpp:49: error: too many initializers for ‘Aisle’
like image 868
Bitdiot Avatar asked Oct 02 '13 19:10

Bitdiot


People also ask

Can you initialize an array in a struct?

An array in a structure is declared with an initial size. You cannot initialize any structure element, and declaring an array size is one form of initialization.

Why can't we initialize members inside a structure?

The direct answer is because the structure definition declares a type and not a variable that can be initialized. Your example is: struct s { int i=10; }; This does not declare any variable - it defines a type.

Can you put arrays in structs?

An array is a collection of data items of the same type. Each element of the array can be int, char, float, double, or even a structure.

Is it possible to initialize structure members while defining a structure?

Structure members cannot be initialized with declaration.


2 Answers

While @us2012 showed what works and provides a good explanation (+1 for him), I find it not very readable. This is an alternative:

Aisle pizza =
    {
        Table {  // Table 0
            Person { 0, "Tom", 100.0 },
            Person { 1, "Mary", 101.0 },
            Person { 2, "Jane", 103.0 },
            Person { 3, "Joe",  104.0 }
        },

        Table {  // Table 1
            Person { 0, "Tom", 100.0 },
            Person { 1, "Mary", 101.0 },
            Person { 2, "Jane", 103.0 },
            Person { 3, "Joe",  104.0 }
        },

        Table {  // Table 2
            Person { 0, "Tom", 100.0 },
            Person { 1, "Mary", 101.0 },
            Person { 2, "Jane", 103.0 },
            Person { 3, "Joe",  104.0 }
        }
    };
like image 80
Daniel Frey Avatar answered Sep 20 '22 01:09

Daniel Frey


You're missing lots of pairs of parentheses. I have added comments to make it clearer which bit starts where.

To put it into one sentence, your problem is that an array with three elements can be initialized with {1,2,3} while a struct that contains an array as its single member is an extra layer and therefore has to be initalized with { {1,2,3} } - the outer layer is the struct, the inner layer is the array.

Aisle pizza =
    { // Aisle init
      { // Table ais[3] init
        {  // ais[0] init
         {  // Person tab[4] init
            { 0, "Tom", 100.0 },
            { 1, "Mary", 101.0 },
            { 2, "Jane", 103.0 },
            { 3, "Joe",  104.0 }
         }
        },

        {  // ais[1] init
         {  // Person tab[4] init
            { 0, "Tom", 100.0 },
            { 1, "Mary", 101.0 },
            { 2, "Jane", 103.0 },
            { 3, "Joe",  104.0 }
         }
        },

        {  // ais[2] init
         {  // Person tab[4] init
            { 0, "Tom", 100.0 },
            { 1, "Mary", 101.0 },
            { 2, "Jane", 103.0 },
            { 3, "Joe",  104.0 }
         }
        }
      }
    };
like image 21
us2012 Avatar answered Sep 18 '22 01:09

us2012