努力学习的kiki 2019-10-06 00:26 采纳率: 100%
浏览 698
已采纳

二进制“<<”:没有找到接受“Time”类型的右操作数的运算符?

先说明具体问题

    Time aida(3, 35);
    Time temp;
    temp = aida*1.17;
    cout << "Aida * 1.17 = " << temp << endl;
    cout << aida*10.0 << endl;

最后一行的“<<”出现了题目的错误,但是倒数第二行是正常输出的,下面是头文件和源代码

class Time
{
private:
    int hours;
    int minutes;
public:
    Time();
    Time(int h, int min);
    friend Time operator*(Time &a, double n);
    friend ostream & operator<<(ostream &os, Time &a);
};
Time operator*(Time &a, double n)
{
    Time temp;
    long total = a.hours * 60 * n + a.minutes * n;
    temp.hours = total / 60;
    temp.minutes = total % 60;
    return temp;
}
ostream & operator<<(ostream &os, Time &a)
{
    os << a.hours << " hours, " << a.minutes << " minutes.";
    return os;
}

刚开始学不是太懂,希望大家指正一下,谢谢。

  • 写回答

1条回答 默认 最新

  • threenewbee 2019-10-06 09:33
    关注
    #include <iostream>
    
    using namespace std;
    
    
    class Time
    {
    private:
        int hours;
        int minutes;
    public:
        Time() { hours = 0; minutes = 0; }
        Time(int h, int min) { hours = h; minutes = min; }
        Time& operator*(double n);
        friend ostream & operator<<(ostream &os, Time &a);
    };
    
    Time& Time::operator*(double n)
    {
        long total = this->hours * 60 * n + this->minutes * n;
        this->hours = total / 60;
        this->minutes = total % 60;
        return *this;
    }
    ostream & operator<<(ostream &os, Time &a)
    {
        os << a.hours << " hours, " << a.minutes << " minutes.";
        return os;
    }
    
    int main() {
        Time aida(3, 35);
        Time temp;
        temp = aida*1.17;
        cout << "Aida * 1.17 = " << temp << endl;
        cout << aida*10.0 << endl;
        return 0;
    }
    
    

    Aida * 1.17 = 4 hours, 11 minutes.
    41 hours, 50 minutes.

    另外,建议你用英文原版的编译器。什么二进制,是Binary,在这里翻译成二元(运算符)。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题