简体   繁体   English

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

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

I'm new to programming.我是编程新手。 I tried looking at a bunch of similar questions to mine on here, but either they were too advanced for me to understand or didn't quite apply to/help me with my situation.我尝试在这里查看一堆与我类似的问题,但是它们要么太先进,我无法理解,要么不太适用/帮助我解决我的情况。

I just learned about 2-D arrays, so I am doing an assignment about them.我刚刚了解了 2-D arrays,所以我正在做关于它们的作业。 (The assignment is about movie ratings, which will explain the names of the variables/arrays/etc in my code) (作业是关于电影分级的,这将解释我的代码中变量/数组/等的名称)

One of my functions is supposed to assign predetermined values to an integer array.我的一个函数应该将预定值分配给 integer 数组。 However, in this function when I try to initialize the array/assign integers to it, I get one of those red squiggly lines which says "Too many initializer values" when I hover over it.但是,在这个 function 中,当我尝试初始化数组/为其分配整数时,当我在它上面使用 hover 时,我得到其中一条红色波浪线,上面写着“初始化器值太多”。

  1. Why does this show up when there AREN'T too many values being put into the array?当数组中没有太多值时,为什么会出现这种情况? (see code below) (见下面的代码)

    I think I've seen in other posts/answers that you can't assign to an array--is this true/is this what's going wrong?我想我在其他帖子/答案中看到了您无法分配给数组的内容——这是真的吗/这是出了什么问题吗? If so, why?如果是这样,为什么? And how else might I put values into this array, if not by just doing something like array[x][y]={...}?如果不只是通过执行诸如 array[x][y]={...} 之类的操作,我还能如何将值放入这个数组中?

  2. Have I used/passed the correct arguments/parameters into the function?我是否使用/传递了正确的参数/参数到 function 中?

Here is some of my code (not my complete program, just the parts relevant to this question):这是我的一些代码(不是我的完整程序,只是与此问题相关的部分):

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

The exact errors I get when I try to compile it are:当我尝试编译它时,我得到的确切错误是:

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

and

message : The initializer contains too many elements

I'm using Microsoft Visual Studio.我正在使用 Microsoft Visual Studio。 I've tried using my book/looking it up, but I'm having a hard time finding the answer(s) I'm looking for.我已经尝试使用我的书/查找它,但我很难找到我正在寻找的答案。 Feel free to correct anything I say incorrectly.随时纠正我所说的任何错误。 I'm here to learn!我是来学习的!

You can use initializer list only for initialization, and not for assignment.您只能将初始化列表用于初始化,而不能用于赋值。 This means you'll have to assign values when you're initialising the array for the first time, ie,这意味着您必须在第一次初始化数组时分配值,即

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

Also, try to use another variable name, since array is a reserved word in C++ (although it is not a problem but it isn't a good coding practice).另外,尝试使用另一个变量名,因为array是 C++ 中的保留字(虽然这不是问题,但它不是一个好的编码习惯)。

the problem with your code is that you can only do the assignment bellow in the variable declaration, ie, you can only do multiple assignment of the array, when you declare the array.你的代码的问题是你只能在变量声明中进行赋值,也就是说,你只能在声明数组时对数组进行多次赋值。

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

In your case the variable array its already declared so the only way to assign values to the different indexes its by iterating or directly assigning a value to each position.在您的情况下,变量array已经声明,因此通过迭代或直接为每个 position 分配值来为不同索引分配值的唯一方法。

The assignment you're trying to do only works if it was something like this:您尝试执行的任务仅在如下情况下才有效:

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

In the variable declaration.在变量声明中。

If you still want to use the void initialRatings(int array[][NUM_MOVIES]) function here is one alternative.如果您仍然想使用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
}

And if you want to see the result after calling the initialRatings function print the values of each position to the console.如果您想在调用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;
}

In C++, when you use arr[i][j] , you are actually accessing a single element of that array.在 C++ 中,当您使用arr[i][j]时,实际上是在访问该数组的单个元素。 Basically, arr[i][j] is actually *(*(arr+i) + j) .基本上, arr[i][j]实际上是*(*(arr+i) + j) And * here is used to dereference a pointer.而这里的*用于取消引用指针。

Let's take a simpler example: suppose you have an array int a[3] = {4, 5, 6};让我们举一个更简单的例子:假设你有一个数组int a[3] = {4, 5, 6}; , there will be 3 memory "blocks" allocated to hold the values, each taking 4 bytes (suppose 1 int takes 4 bytes). ,将分配 3 个 memory “块”来保存值,每个占用 4 个字节(假设 1 个int占用 4 个字节)。

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

For pointers, you can think them as some special variables which hold other variable's memory addresses.对于指针,您可以将它们视为一些特殊变量,其中包含其他变量的 memory 地址。 As the name pointer suggests, they are used to point to other variables.正如名称pointer所暗示的,它们用于指向其他变量。

In C/C++, array names are actually const pointers.在 C/C++ 中,数组名实际上是 const 指针。 And you can use arr+i to point to any one of the 3 blocks.您可以使用arr+i指向 3 个块中的任何一个。

*(arr+i)

Adding a dereference mark * helps you to fetch the actual value stored in that block, eg *(arr+1) == 5 .添加取消引用标记*可帮助您获取存储在该块中的实际值,例如*(arr+1) == 5 What's more, arr[i] is actually another form of *(arr+i) .更重要的是, arr[i]实际上是*(arr+i)的另一种形式。

So that's what happened behind when you write array assignment statements like arr[i] = 1 .这就是当你编写像arr[i] = 1这样的数组赋值语句时发生的事情。 Things get a bit complicated from 1d to 2d.从 1d 到 2d,事情变得有点复杂。 But the genenal idea is the same: when you assign to arr[i][j] , you are writing values to one certain location of the 2d array.但是一般的想法是相同的:当您分配给arr[i][j]时,您正在将值写入二维数组的某个特定位置。

And that's why you get the error error C2440: '=': cannot convert from 'initializer list' to 'int' .这就是为什么您会收到错误error C2440: '=': cannot convert from 'initializer list' to 'int'

As for initializer list , as the name suggests, it's used at initialzing time.至于initializer list ,顾名思义,它是在初始化时使用的。

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

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