hahastephanie 2019-10-16 23:47 采纳率: 0%
浏览 765

运算符重载must take either zero or one argument?

C++中运算符+重载出错

//Time.h
#ifndef TIME_H
#define TIME_H
#include<iostream>
using namespace std;

class Time
{
public:
    Time(int H=0,int M=0,int S=0);
    void SetTime(int H,int M,int S);
    Time operator+(const Time &t, int n);
    friend ostream & operator<<(ostream &out,const Time &t);
private:
    int h,m,s;
};
#endif

//Time.cpp
#include<iostream>
#include"Time.h"
using namespace std;

Time::Time(int H,int M,int S):h(H),m(M),s(S){}
void Time::SetTime(int H,int M,int S)
{
    h=H,m=M,s=S;
}

Time Time::operator+(const Time &t, int n)
{
    Time temp(t);
    int y=n+s+60*(m+60*h);
    temp.s=y%60;
    temp.m=y/60%60;
    temp.h=y/3600;
    return temp;
}

ostream & operator<<(ostream &out,const Time &t)
{
    out<<t.h<<":"<<t.m<<"."<<t.s;
    return out;
}

//Test.cpp
#include<iostream>
#include"Time.h"
using namespace std;

int main()
{
    Time t1,t2;
    t1.SetTime(23,10,29);
    t2.SetTime(10,4,23);
    cout<<t1+3;
    return 0;
}

图片说明

  • 写回答

3条回答 默认 最新

  • leishapolang33 2019-10-17 09:49
    关注

    我的理解是重载operator+,相当于timeObj1+timeObj2,如果增加了参数int n,这个表达式没办法展开,也就形成不了操作符号重载。

    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题