Chinaunix
标题:
操作符重载
[打印本页]
作者:
DraculaW
时间:
2007-01-10 14:03
标题:
操作符重载
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;
}
复制代码
这样子为什么不行呢 呵呵 求助
[
本帖最后由 DraculaW 于 2007-1-10 14:06 编辑
]
作者:
DraculaW
时间:
2007-01-10 14:05
// 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;
}
复制代码
作者:
DraculaW
时间:
2007-01-10 14:53
in >> month >> temp
>> day >> temp
>> year //这里多了一个char
>> hour >> temp
>> minute >> temp
>> second >> temp;
复制代码
呵呵 自己解决了
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2