C ++通过二维数组来funcions [英] c++ passing 2d arrays to funcions

查看:154
本文介绍了C ++通过二维数组来funcions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我的code心不是说完呢我不是要求它做。它应该输入食品3只猴子在一个星期和其他的东西吃。但我已经遇到了障碍。它给了我一个错误(错误:没有操作员<<匹配这些操作数),当我把CIN在poundsEaten功能。我不能传递数组正确的是,为什么它不是工作?感谢您的帮助

 的#include<了iomanip>
#包括<的iostream>
使用名字空间std;

//全局常量
const int的NUM_MONKEYS = 3;
const int的天数= 7;

//原型
无效poundsEaten(const的双重[] [日],INT,INT);
无效averageEaten();
无效至少();
无效大多数();

诠释的main()
{
    // const int的NUM_MONKEYS = 3;
    // const int的天数= 7;
    双foodEaten [NUM_MONKEYS] [日]; // 3行,7列阵列

    poundsEaten(foodEaten,NUM_MONKEYS,天);

    系统(暂停);
    返回0;
}

无效poundsEaten(const的双阵列[] [日],int的列,诠释COLS)
{
    对于(INT指数= 0;指数<行;指数++)
    {
        对于(诠释计数= 0; COUNT<天;计数++)
        {
            COUT<< &LT食物吃一天中的英镑;< (指数+ 1);
            COUT<< 以猴<< (计数+ 1);
            CIN>>数组[索引] [统计]
            //这是我得到的错误
        }
    }
}
 

解决方案

声明:

 常量双阵列[] [日]
 

不过, poundsEaten 函数里,你问用户输入的信息填写在阵列,这意味着在阵列不是const的,因此,错误。从参数中删除常量预选赛使得阵列可以通过用户输入来改变。

 无效poundsEaten(双阵列[] [日],int的列,诠释COLS)
 

顺便说一句:请不要使用阵列为变量名的数组,用一些其他的名字很好的做法。 同时, COLS 不是内部使用的 poundsEaten 的功能。

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

解决方案

you declare:

const double array[][DAYS],

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)

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 ++通过二维数组来funcions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆