简体   繁体   English

在结构中的数组中的结构中初始化数组时出现问题

[英]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++. 我想我不知道如何用C ++初始化东西。 Any help would be appreciated. 任何帮助,将不胜感激。

I use g++ like so: g++ init_arr.cpp 我像这样使用g ++:g ++ init_arr.cpp

Here's the code. 这是代码。 I want to initialize all the people at all the tables in Aisle pizza: 我想在Aisle比萨的所有桌子上初始化所有人:

// 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’

While @us2012 showed what works and provides a good explanation (+1 for him), I find it not very readable. 虽然@ us2012展示了什么有效,并提供了一个很好的解释(+1给他),但我觉得它不太可读。 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 }
        }
    };

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. 把它放到一个句子中,你的问题是可以使用{1,2,3}初始化具有三个元素的数组,而包含数组作为其单个成员的结构是一个额外的层,因此必须使用{1,2,3}初始化{ {1,2,3} } - 外层是struct ,内层是数组。

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

Each block needs to represent an object. 每个块都需要表示一个对象。 Aisle struct contains an array object (ais). Aisle struct包含一个数组对象(ais)。 Each element of the ais array contains a Table struct. ais数组的每个元素都包含一个Table结构。 Each Table struct contains an array object (tab). 每个Table结构都包含一个数组对象(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]
                }
            }

        }
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM