关于重载<<运算符的问题
#include <iostream>using namespace std;
class Clock
{
public:
Clock(int hour=0, int minute=0, int second=0):hour(hour), minute(minute), second(second) {}
//void showTime();
Clock& operator++ ();
Clock operator++ (int);
friend ostream & operator<< (ostream &out,const Clock &c);
private:
int hour, minute, second;
};
//void Clock::showTime()
//{
// cout<<hour<<":"<<minute<<":"<<second<<endl;
//}
ostream & operator<< (ostream &out ,const Clock &c)
{
out<<c.hour<<":"<<c.minute<<":"<<c.second<<"\n";
return out;
}
Clock &Clock::operator++()
{
second++;
if(second>=60)
{
minute++;
second = second%60;
if(minute>=60)
{
hour = (hour+1)%24;
minute = minute%60;
}
}
return * this;
}
Clock Clock::operator++(int)
{
Clock old = * this;
++ ( * this);
return old;
}
int main()
{
Clock c(23,58,59);
cout<<c;
//c.showTime();
//(c++).showTime();
//(++c).showTime();
return 0;
}
程序有错,求大神解