7

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’
3
  • 8
    +1 for creating a minimal test case. Commented Oct 2, 2013 at 19:51
  • 1
    You just missed a lot of braces. As a help for understanding consider the case that Aisle had more members than ais. Where would you put these members in? Commented Oct 2, 2013 at 19:59
  • Thanks for the +1. I try to help my helpers as much as I can. Often doing the additional work usually leads to the answer, and if not, helps me remember the answer when someone gives it! Commented Oct 3, 2013 at 11:35

3 Answers 3

7

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 }
        }
    };
Sign up to request clarification or add additional context in comments.

4 Comments

This is a very lucid answer. Thanks for it.
I'd like to give you a check mark too. I gave it to the lots of braces answer, because he sorta showed my thinking mistake in terms of tracking my braces. But this is definitely as good an answer. So thanks again.
@Bitdiot I agree that an answer which explains the braces-stuff should be picked, but I would've picked us2012's answer as he was faster and IMHO explains it better. But that's of course your choice.
You are right. I switched checks. I didn't notice it, but now I see it. Thanks for pointing that out to me.
5

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

1 Comment

Thanks. For the answer. And thanks for pointing out my misunderstanding. I gave you the check mark!
2

Each block needs to represent an object. Aisle struct contains an array object (ais). Each element of the ais array contains a Table struct. Each Table struct contains an array object (tab). and so on...

Try this:

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

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

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

        }
    };

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.