错误:"与'运算符<<'不匹配(操作数类型为 std::istream)

Error : "No match for 'operator <<' (operand types are std::istream)

本文关键字:lt 类型 std istream 操作数 运算符 错误 不匹配      更新时间:2023-10-16

这是我的第一个StackOverflow,很高兴加入。我目前正在用Stroustrup的"使用C++编程和实践"学习C++,目前我们正在使用他创建的库(#include"std_lib_facilities.h")

我完成了一个练习,犯了这个错误"与'operator<<'不匹配(操作数类型为std::istream)"

我写的程序可能还不完美,但我现在甚至无法编译它。我在其他帖子中读过一些关于"重载运算符"的内容,但我不明白它的含义,也不明白为什么在我的其他文件中,即使加载了相同的库,也不会发生这种情况。

#include "../../std_lib_facilities.h"
int main()
{
    double val1 = 0;
    double val2 = 0;
    cin << val1 << val2 << endl;
    if (val1>val2)
        cout << val1 << " is larger than " << val2 <<endl;
    else
        cout << val1 << " is smaller than " << val2 <<endl;

}

您应该在c++中使用>>运算符进行输入。这被称为提取运算符,用于从键盘或其他类似文件中获取值。语法为:

int variableName;
cout << "Enter number";
cin >>  variableName;

<lt;operator是插入运算符,用于在屏幕或文件上写入数据。

您应该将operator>>std::cin一起使用,后者是std::istream,不支持operator<<

std::cin >> val1 >> val2;