首页 > 解决方案 > 没有运算符 ">>" 匹配这些操作数操作数类型是:std::istream>>int

问题描述

我是计算机科学专业的学生,​​刚开始编程。我的代码有问题。我已经寻找了所有可用的类似解决方案,但没有一个能解决我的问题。这是我的二维数组程序代码

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main(void)
{

    int arr[4][5];//A 2D array is taken with 4 fields and 5 tests
    int i, j = 0, p, test_value, test_no, max, max_occ, max_occ2, pos1, pos2, temp, flag, total;

    float avg;
    for (i = 0; i<4; i++)
        for (j = 0; j<5; j++)
            arr[i][j] = -1; //Values of each element is initialized to -1;
    while (1) {
        cout << "\nPress 1 to enter a new testvalue."
            << "\nPress 2 to count the total number of occurance on the table."
            << "\nPress 3 to Delete a field."
            << "\nPress 4 to Find Max,and 2nd Max occured element."
            << "\nPress 5 to average of all fields."
            << "\nPress 6 to Find field with max value."
            << "\nPress 7 to exit.";
        cin >> p;
        switch (p)
        {
        case 1: flag = 0;
            cout << "\nEnter new test value:";
            cin >> test_value;
            for (i = 0; i<4; i++)
            {
                for (j = 0; j<5; j++)
                {
                    if (arr[i][j] == -1) //If the table is empty a test result is entered
                    {
                        flag = 1;
                        arr[i][j] = test_value;
                        break;
                    }
                }
                if (flag == 1)
                    break; //If one value is entered in the table the the loop stops
            }
            break;
        case 2: test_no = 0;
            for (i = 0; i<4; i++)
            {
                for (j = 0; j<5; j++)
                {
                    if (arr[i][j] != -1)
                    {
                        cout << arr[i][j];//Prints all entered value of the table
                        test_no += 1; //counts them
                    }
                }
            }
            cout << "\nTotal number of tests:" << test_no;
            break;

        case 3: cout << "\nEnter the field value: ";
            This Line -->cin >> i - 1;
            cout << "\nEnter the test number: ";
            This Line --> cin >> j - 1;
            arr[i][j] = -1; //Initialize the deleted position with -1;
            break;
        case 4: pos1 = pos2 = 0;
            max_occ = max_occ2 = 0;
            for (i = 0; i<4; i++)
            {

                for (j = 0; j<5; j++)
                {
                    max = 0;
                    if (arr[i][j] != -1)
                    {

                        temp = arr[i][j];
                        if (temp != pos1 && temp != pos2)
                        {
                            max = std::count(&arr[0][0], &arr[3][5], temp); //count () is a inbuilt funtion
                                                                            //count(starting address,ending address ,value in array)
                            if (max_occ<max)
                            {
                                if (max_occ2 != 0)
                                {
                                    max_occ2 = max_occ;
                                    pos2 = pos1;
                                }
                                max_occ = max;
                                pos1 = temp;
                            }
                            else if (max_occ2<max)
                            {
                                max_occ2 = max;
                                pos2 = temp;
                            }
                        }
                    }

                }
            }
            cout << "Maximum Occurance of " << pos1 << " is :" << max_occ << endl;
            cout << "2nd maximum occurance of " << pos2 << " is:" << max_occ2 << endl;
            break;
        case 5:avg = 0;
            for (i = 0; i<4; i++)
            {
                avg = 0;
                for (j = 0; j<5; j++)
                {
                    if (arr[i][j] != -1)
                        avg += arr[i][j]; //add all the values of fields test
                }
                cout << "\nAverage of field " << i + 1 << ":" << avg / 5; //prints the average of five tests
            }
            break;
        case 6:total = 0;
            max = 0;
            pos1 = 0;
            for (i = 0; i<4; i++)
            {
                for (j = 0; j<5; j++)
                {
                    if (arr[i][j] != -1)
                        total += arr[i][j]; //add all the elements of row
                }
                if (max<total)
                {
                    max = total;//Finds maximum of all the fields
                    pos1 = i;//stores the index of maximum field
                }
            }
            cout << "\nThe field with max score:" << pos1 + 1 << endl;
            break;


        case 7: exit(0);
        }
    }

    return 0;
}

我用“ This--> ”提到了两行我有一个错误说

Error   C2678   binary '>>': no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)

请有人可以帮我解决这个问题。

我真的会给予任何帮助。

标签: c++operator-overloadingstd

解决方案


在这些行中

cin >> i - 1;

cin >> j - 1;

右操作数是不能绑定到非常量引用的临时对象。

这些说法没有任何意义。也许你的意思是

cin >> i;
i = i - 1;

cin >> j;
j = j - 1;

推荐阅读