C++ 将二维数组传递给函数 [英] c++ passing 2d arrays to funcions
问题描述
我知道我的代码还没有完成,但我并不要求它完成.它应该输入3只猴子一个星期吃的食物和其他东西.但我遇到了障碍.当我将 cin 放入磅Eaten函数时,它给了我一个错误(错误:没有运算符<<"匹配这些操作数).我没有正确传递数组是因为它不起作用吗?感谢您的帮助
I know my code isnt finished yet im not asking for it to be done. It's supposed to input food eaten by 3 monkeys over a week and other stuff. But I've hit a snag. It gives me an error(Error: no operator "<<" matches these operands) when i put the cin in the poundsEaten function. Am I not passing the array right is that why it isnt working? Thanks for any help
#include <iomanip>
#include <iostream>
using namespace std;
//Global Constants
const int NUM_MONKEYS = 3;
const int DAYS = 7;
//Prototypes
void poundsEaten(const double[][DAYS],int, int);
void averageEaten();
void least();
void most();
int main()
{
//const int NUM_MONKEYS = 3;
//const int DAYS = 7;
double foodEaten[NUM_MONKEYS][DAYS]; //Array with 3 rows, 7 columns
poundsEaten(foodEaten, NUM_MONKEYS, DAYS);
system("pause");
return 0;
}
void poundsEaten(const double array[][DAYS], int rows, int cols)
{
for(int index = 0; index < rows; index++)
{
for(int count = 0; count < DAYS; count++)
{
cout << "Pounds of food eaten on day " << (index + 1);
cout << " by monkey " << (count + 1);
cin >> array[index][count];
// Here is where i get the error
}
}
}
推荐答案
您声明:
const double array[][DAYS],
然而,在poundsEaten
函数中,你要求用户输入信息来填充array
,这意味着array
不是const,因此,错误.从参数中删除 const
限定符,以便用户输入可以更改 array
.
however, inside poundsEaten
function, you are asking user to input information to fill in the array
, which means the array
is not const, therefore, error. Remove the const
qualifier from the parameter such that the array
can be changed by user input.
void poundsEaten(double array[][DAYS], int rows, int cols)
顺便说一句:不要使用 array
作为数组的变量名,使用其他一些名称以获得良好的实践.同时,cols
未在您的 poundsEaten
函数中使用.
BTW: don't use array
as variable name for an array, use some other names for good practice.
Meanwhile,cols
is not used inside your poundsEaten
function.
这篇关于C++ 将二维数组传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!