我是编程新手。我尝试着看一堆类似的问题,但我觉得这些问题太高级了,以至于我无法理解,或者不能完全适用于我/我的情况。
我刚刚了解了二维数组,因此我正在对它们进行分配。 (该作业是关于电影分级的,它将解释我代码中变量/数组/等的名称)
我的函数之一应该将预定值分配给整数数组。但是,在此函数中,当我尝试向其初始化数组/分配整数时,将鼠标悬停在上面时,会得到一条红色的弯曲行,其中一条显示“初始化程序值太多”。
我想我在其他无法分配给数组的帖子/答案中看到了-这是真的/这是怎么回事?如果是这样,为什么?如果不仅仅通过执行array [x] [y] = {...}之类的方法,我还可以将值放入该数组吗?
这是我的一些代码(不是我的完整程序,只是与此问题相关的部分):
#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。我曾经尝试使用我的书/查找它,但是我很难找到想要的答案。随意纠正我讲错的任何话。我在这里学习!
最佳答案
代码的问题在于,您只能在变量声明中进行赋值,即在声明数组时只能对数组进行多次赋值。
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
已经声明,因此是通过迭代或直接将值分配给每个位置将值分配给不同索引的唯一方法。您尝试执行的任务只有在如下情况下才有效:
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])
函数,则这里是一种替代方法。
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
函数之后查看结果,请将每个位置的值打印到控制台。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++ - 二维数组— “Too many initializer values”警告/错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63296606/