【问题标题】:no operator ">>" matches these operands -- operand types are: std::istream >> const double没有运算符 ">>" 匹配这些操作数 -- 操作数类型是:std::istream >> const double
【发布时间】:2021-01-09 06:34:42
【问题描述】:

My Code

#include <iostream>
#include <stdlib.h>
#define Pi 3.14159
 
using namespace std;
 
class Rectangle
{
    public:
        friend ostream &operator<<(ostream &output,const Rectangle &D)
        {
            output<<"length:"<<D.length<<endl<<"height:"<<D.height<<endl;
            return output;
        }
        friend istream &operator>>(istream &input,const Rectangle &D)
        {
            input >> D.length >> D.height;
            return input;
        }
    private:
        double length;
        double height;
};

错误在 [17,19]:no operator ">>" 匹配这些操作数 -- 操作数类型是:std::istream >> const double

我在网上搜索了很长时间。但是没有用。请帮助或尝试提供一些想法如何实现这一目标。 非常感谢您的回答。

【问题讨论】:

  • 您没有将变量声明为要写入值的const &amp;
  • const Rectangle &amp;D 表示您不能修改D 或其成员。

标签: c++


【解决方案1】:

问问自己,你能 const 变量吗?读取到变量会改变变量,所以答案显然是否定的。

试试这个

friend istream &operator>>(istream &input,Rectangle &D)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多