繁体   English   中英

2-D Array — “Too many initializer values”警告/错误

[英]2-D Array — “Too many initializer values” warning/error

我是编程新手。 我尝试在这里查看一堆与我类似的问题,但是它们要么太先进,我无法理解,要么不太适用/帮助我解决我的情况。

我刚刚了解了 2-D arrays,所以我正在做关于它们的作业。 (作业是关于电影分级的,这将解释我的代码中变量/数组/等的名称)

我的一个函数应该将预定值分配给 integer 数组。 但是,在这个 function 中,当我尝试初始化数组/为其分配整数时,当我在它上面使用 hover 时,我得到其中一条红色波浪线,上面写着“初始化器值太多”。

  1. 当数组中没有太多值时,为什么会出现这种情况? (见下面的代码)

    我想我在其他帖子/答案中看到了您无法分配给数组的内容——这是真的吗/这是出了什么问题吗? 如果是这样,为什么? 如果不只是通过执行诸如 array[x][y]={...} 之类的操作,我还能如何将值放入这个数组中?

  2. 我是否使用/传递了正确的参数/参数到 function 中?

这是我的一些代码(不是我的完整程序,只是与此问题相关的部分):

#include <iostream>
#include <cstdlib>

using namespace std;

//Global constants
const int NUM_REVIEWERS = 4;   //Number of rows in reviews array
const int NUM_MOVIES = 6;      //Number of columns in reviews array     

//function prototype
void initialRatings(int[][NUM_MOVIES]); 

int main()
{
    // Variable declarations
    int someArray[NUM_REVIEWERS][NUM_MOVIES];  // Ratings for reviewers
    int choice;

    initialRatings(someArray); //function call with actual argument passing in the array and the number of rows

    return 0;
}


/***************************************************************************
    function definition for initialRatings

    this function sets all data elements in the 2-D array to the sample data

****************************************************************************/

void initialRatings(int array[][NUM_MOVIES])
{
    array[NUM_REVIEWERS][NUM_MOVIES] = { {3, 1, 5, 2, 1, 5 }, 
                                         {4, 2, 1, 4, 2, 4 },//the beginning of this row is where I get the error 
                                         {3, 1, 2, 4, 4, 1 }, 
                                         {5, 1, 4, 2, 4, 2 } };
}

当我尝试编译它时,我得到的确切错误是:

error C2440: '=': cannot convert from 'initializer list' to 'int'

message : The initializer contains too many elements

我正在使用 Microsoft Visual Studio。 我已经尝试使用我的书/查找它,但我很难找到我正在寻找的答案。 随时纠正我所说的任何错误。 我是来学习的!

您只能将初始化列表用于初始化,而不能用于赋值。 这意味着您必须在第一次初始化数组时分配值,即

int array[NUM_REVIEWERS][NUM_MOVIES] = 
      { {3, 1, 5, 2, 1, 5 }, 
      {4, 2, 1, 4, 2, 4 },
      {3, 1, 2, 4, 4, 1 }, 
      {5, 1, 4, 2, 4, 2 } };

另外,尝试使用另一个变量名,因为array是 C++ 中的保留字(虽然这不是问题,但它不是一个好的编码习惯)。

你的代码的问题是你只能在变量声明中进行赋值,也就是说,你只能在声明数组时对数组进行多次赋值。

array[NUM_REVIEWERS][NUM_MOVIES] = 
      { {3, 1, 5, 2, 1, 5 }, 
      {4, 2, 1, 4, 2, 4 },//the beginning of this row is where I get the error 
      {3, 1, 2, 4, 4, 1 }, 
      {5, 1, 4, 2, 4, 2 } };

在您的情况下,变量array已经声明,因此通过迭代或直接为每个 position 分配值来为不同索引分配值的唯一方法。

您尝试执行的任务仅在如下情况下才有效:

int array[NUM_REVIEWERS][NUM_MOVIES] = 
      { {3, 1, 5, 2, 1, 5 }, 
      {4, 2, 1, 4, 2, 4 },//the beginning of this row is where I get the error 
      {3, 1, 2, 4, 4, 1 }, 
      {5, 1, 4, 2, 4, 2 } };

在变量声明中。

如果您仍然想使用void initialRatings(int array[][NUM_MOVIES]) function 这里是另一种选择。


void initialRatings(int array[NUM_REVIEWERS][NUM_MOVIES])
{
    array[0][0] = 3;
    array[0][1] = 1;
    array[0][2] = 5;
    array[0][3] = 2;
    array[0][4] = 1;
    array[0][5] = 5;

    array[1][0] = 4;
    array[1][1] = 2;
    array[1][2] = 1;
    array[1][3] = 4;
    array[1][4] = 2;
    array[1][5] = 4;

//... Do the same for the other rows
}

如果您想在调用initialRatings function 后查看结果,请将每个 position 的值打印到控制台。

int main()
{
    // Variable declarations
    int someArray[NUM_REVIEWERS][NUM_MOVIES];  // Ratings for reviewers
    initialRatings(someArray); //function call with actual argument passing in the array and the number of rows
    for (int row = 0; row < NUM_REVIEWERS; row++)
    {
        for (int column = 0; column < NUM_MOVIES; column++)
        {
            cout << someArray[row][column] << " "; // print the array value and a space to separate 
                                                    // the values in each column
        }
        cout << endl; // add line break for each row
    } 
    return 0;
}

在 C++ 中,当您使用arr[i][j]时,实际上是在访问该数组的单个元素。 基本上, arr[i][j]实际上是*(*(arr+i) + j) 而这里的*用于取消引用指针。

让我们举一个更简单的例子:假设你有一个数组int a[3] = {4, 5, 6}; ,将分配 3 个 memory “块”来保存值,每个占用 4 个字节(假设 1 个int占用 4 个字节)。

|   4    |   5       |   6        |
0x1      0x1 + 4     0x1 +8       0x1+12
arr      arr+1       arr+2        arr+3

对于指针,您可以将它们视为一些特殊变量,其中包含其他变量的 memory 地址。 正如名称pointer所暗示的,它们用于指向其他变量。

在 C/C++ 中,数组名实际上是 const 指针。 您可以使用arr+i指向 3 个块中的任何一个。

*(arr+i)

添加取消引用标记*可帮助您获取存储在该块中的实际值,例如*(arr+1) == 5 更重要的是, arr[i]实际上是*(arr+i)的另一种形式。

这就是当你编写像arr[i] = 1这样的数组赋值语句时发生的事情。 从 1d 到 2d,事情变得有点复杂。 但是一般的想法是相同的:当您分配给arr[i][j]时,您正在将值写入二维数组的某个特定位置。

这就是为什么您会收到错误error C2440: '=': cannot convert from 'initializer list' to 'int'

至于initializer list ,顾名思义,它是在初始化时使用的。

暂无
暂无

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

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