【问题标题】:cin: No Operator Found Which Takes A Left-Hand Operandcin:未找到采用左手操作数的运算符
【发布时间】:2022-01-19 05:30:47
【问题描述】:

我正在尝试获取两个以空格分隔的变量并遇到此错误,以及 [no operator ">>" 匹配这些操作数] 和 [syntax error: ">>"]。

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

...

void player(char array[][ROWS_AND_COLUMNS], char letter)
{
    bool element_occupied = false;

    do
    {
        int row = 0;
        int column = 0;
        cout << "Enter player " << letter << ", row and column:";
        cin >> inputValidate(row, 1, 3) >> inputValidate(column, 1, 3);

        if (array[(row - 1)][(column - 1)] == '*')
        {
            array[(row - 1)][(column - 1)] = letter;
            element_occupied = true;
        }
        else
        {
            cout << "Sorry, that spot is taken." << endl;
            element_occupied = false;
        }

    } while (element_occupied == false);
}

inputValidate() 取输入、最小值和最大值,如果是 1、2 或 3,则返回输入值。

编辑:

int inputValidate(int user_number, int lowest, int highest)
{

    while (!(cin >> user_number) || (user_number < lowest || user_number > highest))
    {
        cout << "Error. Enter a number from " << lowest << " to " << highest << ": ";
        cin.clear();
    }

    return user_number;
}

这是一个井字游戏。

【问题讨论】:

  • 用你自己的话来说,cin &gt;&gt; inputValidate(row, 1, 3) &gt;&gt; inputValidate(column, 1, 3); 是什么意思?特别是:应该读入哪些变量(由于从标准输入读取数据而改变)?
  • 请显示实际且完整的错误信息。 inputValidate() 实际上是什么样子的?更重要的是,它究竟返回了什么? inputValidate() 是普通函数还是流操纵器?我感觉我知道答案,但我需要查看更多代码才能确定。请提供minimal reproducible example
  • cin 应该接收两个以空格分隔的整数并将它们读入行和列。 inputValidate 是一个 int 函数;我已经附在上面了。
  • 代码是“C2678 binary '>>': 没有找到接受'std::istream' 类型左侧操作数的运算符(或者没有可接受的转换)”。

标签: c++ syntax cin


【解决方案1】:

inputValidate() 返回一个 临时 int,一个右值,operator&gt;&gt; 无法读取。相反,它需要一个左值(即,具有名称的变量)。这就是您在 cin &gt;&gt; inputValidate(...) 语句中遇到错误的原因。

即使该语句可以编译,使用它也没有任何意义。 inputValidate() 自己读取cin,所以player() 不应该尝试将inputValidate() 的返回值传递给cin &gt;&gt;inputValidate() 会读入一个经过验证的数字并将其返回,然后player 将尝试读入一个新的未经验证的数字以覆盖它。

就此而言,传递user_number 参数根本没有意义。您在 按值 中传递它,只是为了立即用用户输​​入覆盖它。所以player()传入的值是无关紧要的。如果您想return 用户的输入,那么user_number 应该是inputValidate() 内的局部变量。否则,它应该是一个通过引用传递的输出参数,而inputValidate()的返回值应该是void

此外,当operator&gt;&gt; 由于输入无效而在inputValidate() 内部失败时,您不会丢弃导致其失败的输入。 cin.clear() 仅重置流的错误状态,不会丢弃任何数据。为此,您需要致电 cin.ignore()

话虽如此,请尝试更多类似的东西:

#include <iostream>
#include <string>
#include <vector>
#include <limits>
using namespace std;

...

int inputValidate(int lowest, int highest)
{
    int user_number;

    do
    {
        if (!(cin >> user_number))
        {
            cout << "Invalid. Enter a valid number: ";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
        else if (user_number < lowest || user_number > highest)
        {
            cout << "Error. Enter a number from " << lowest << " to " << highest << ": ";
        }
        else break;
    }
    while (true);

    return user_number;
}

void player(char array[][ROWS_AND_COLUMNS], char letter)
{
    int row, column;

    do
    {
        cout << "Enter player " << letter << ", row and column:";
        row = inputValidate(1, 3) - 1;
        column = inputValidate(1, 3) - 1;

        if (array[row][column] == '*')
            break;

        cout << "Sorry, that spot is taken." << endl;
    }
    while (true);

    array[row][column] = letter;
}

【讨论】:

    猜你喜欢
    • 2018-10-12
    • 1970-01-01
    • 1970-01-01
    • 2018-08-13
    • 2014-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多