读书人

iostream重载解决思路

发布时间: 2012-03-01 10:25:47 作者: rapoo

iostream重载
CODE:
[Copy to clipboard]
class Date{
int month;
int day;
int year;
int hour;
int minute;
int second;
// and the get set function
}

// the iostream
// format: mm/dd/yyyy hh:mm:ss \n

//mm/dd/yyyy hh:nn:ss \n
ostream &operator < <(ostream& out, const Date& date){
out < < date.getMonth() < < "/ "
< < date.getDay() < < "/ "
< < date.getYear() < < " "
< < date.getHour() < < ": "
< < date.getMinute() < < ": "
< < date.getSecond() < < " " < < endl;
}

istream &operator> > (istream& in, Date& date){
char temp;
int month, day, year, hour, minute, second;
in > > month > > temp
> > day > > temp
> > year > > temp
> > hour > > temp
> > minute > > temp
> > second > > temp;

date.setMonth(month);
date.setDay(day);
date.setYear(year);
date.setHour(hour);
date.setMinute(minute);
date.setSecond(second);
cout < < endl;
}
这样子为什么不行呢 呵呵 求助

// test code
stringstream s;
s < < "8/17/1976 1:2:3\n ";

Date d;
s > > d;

if (d.getMonth() == 8 &&
d.getDay() == 17 &&
d.getYear() == 1976 &&
d.getHour() == 1 &&
d.getMinute() == 2 &&
d.getSecond() == 3) {



return true;
}
else {
return false;
}

[解决办法]
> > year > > hour > > temp

[解决办法]
stringstream
和ostream,istream的继承关系

读书人网 >C++

热点推荐