问题遇到的现象和发生背景
时间类运算符重载"<<"出错
用代码块功能插入代码,请勿粘贴截图
```c++
#include<iostream>
using namespace std;
class Time{
friend istream & operator>>(istream &in,Time &t)
{
int h,m,s;
cin>>h>>m>>s;
t.Hour=h;
t.Minute=m;
t.Second=s;
return in;
}
friend ostream & operator<<(ostream & out,Time&t)
{
cout<<t.Hour<<':'<<t.Minute<<':'<<t.Second<<endl;
return out;
}
public:
Time();
Time(int NewH,int NewM,int NewS);
void ShowTime();
void ShowTime(int i);
// void SetTime(int,int,int);
Time&operator++();//前置++
const Time operator++(int);//后置++
Time operator>>(Time &t);
~Time(){cout<<"GoodBye!"<<endl;
}
private:
int Hour,Minute,Second;
};
Time::Time(int NewH,int NewM,int NewS){Hour=NewH<0||NewH>23?0:NewH;
Minute=NewM<0||NewM>59?0:NewM;
Second=NewS<0||NewS>59?0:NewS;
}
Time::Time(){Hour=0;
Minute=0;
Second=0;}
void Time::ShowTime()
{
cout<<Hour<<":"<<Minute<<":"<<Second<<endl;
}
//void Time::SetTime(int NewH,int NewM,int NewS){
// Hour=NewH<0||NewH>23?0:NewH;
// Minute=NewM<0||NewM>59?0:NewM;
// Second=NewS<0||NewS>59?0:NewS;
//}
void Time::ShowTime(int i){
cout<<(Hour>12?Hour-12:Hour)<<":"<<Minute<<":"<<Second;
cout<<(Hour>12?"PM":"AM")<<endl;
}
Time&Time::operator++()//前置++
{
Second++;
if(Second>=60){
Second=Second%60;
++Minute;
if(Minute>=60){
Minute=Minute%60;
++Hour;
}
}
return *this;
}
const Time Time::operator++(int){
Time t=*this;
Second+=1;
if(Second>60){
Second=Second%60;
++Minute;
if(Minute>=60){
Minute=Minute%60;
++Hour;
}
}
}
int main(){
Time myTime,t;
// myTime.SetTime(8,30,30);
// myTime.ShowTime();
// myTime.SetTime(20,29,59);
// myTime.ShowTime();
// ++myTime;
// myTime.ShowTime();
// myTime++;
cin<<t;
cout<<myTime;
return 0;
}
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/576858745766164.png "#left")