C++ - 数组的初始化器太多

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12335747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:10:36  来源:igfitidea点击:

C++ - Too Many Initializers for Arrays

c++cmultidimensional-arrayinitialization

提问by Xelza

I have made an array like this but then it keeps saying I had too many initializers. How can I fix this error?

我做了一个这样的数组,但它一直说我有太多的初始化程序。我该如何解决这个错误?

        int people[6][9] = {{0,0,0,0,0,0},
                        {0,0,0,0,0,0},
                        {0,0,0,0,0,0},
                        {0,0,0,0,0,0},
                        {0,0,0,0,0,0},
                        {0,0,0,0,0,0},
                        {0,0,0,0,0,0},
                        {0,0,0,0,0,0},
                        {0,0,0,0,0,0}};

采纳答案by Jonathan Leffler

int people[6][9] =
{
    {0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0},
};

Arrays in C are in the order rows then columns, so there are 6 rows of 9 integers, not 9 rows of 6 integers in the initializer for the array you defined.

C 中的数组按行然后列的顺序排列,因此您定义的数组的初始值设定项中有 6 行 9 个整数,而不是 9 行 6 个整数。

回答by TheAJ

The issue here is that you have the rows/columns indices swapped in the array declaration part, and thus the compiler is confused.

这里的问题是您在数组声明部分交换了行/列索引,因此编译器很困惑。

Normally when declaring a multi-dimensional array, first index is for rows, second is for columns.

通常在声明多维数组时,第一个索引用于行,第二个用于列。

This form should fix it:

这个表格应该修复它:

   int people[9][6] = {{0,0,0,0,0,0},
                    {0,0,0,0,0,0},
                    {0,0,0,0,0,0},
                    {0,0,0,0,0,0},
                    {0,0,0,0,0,0},
                    {0,0,0,0,0,0},
                    {0,0,0,0,0,0},
                    {0,0,0,0,0,0},
                    {0,0,0,0,0,0}};

回答by Rapptz

You mixed the 6 and the 9 in the indexes.

您在索引中混合了 6 和 9。