📄 subject_65789.htm
字号:
<p>
序号:65789 发表者:everfly 发表日期:2003-12-19 14:41:56
<br>主题:关于 <<的第一种重载版本 的问题
<br>内容:<<的第一种重载版本<BR>我是这样做的,在类里面声明friend void operator<< (ostream & os, const Time & t );<BR>然后象下面一样重载:<BR>void operator<< (ostream & os, const Time & t )<BR>{<BR> os << t.hours << " hours, " << t.minutes << " minutes";<BR>}<BR><BR>为什么出现错误呢?
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:everfly 回复日期:2003-12-19 15:00:30
<br>内容:贴上原代码如下:<BR>// mytime0.h -- Time class before operator overloading<BR>#ifndef MYTIME0_H_<BR>#define MYTIME0_H_<BR>#include <iostream><BR>using namespace std;<BR><BR>class Time{<BR>private:<BR> int hours;<BR> int minutes;<BR><BR>public:<BR> Time ();<BR> Time (int h,int m = 0 );<BR> void AddMin (int m );<BR> void AddHr (int h );<BR> void Reset (int h = 0,int m = 0 );<BR> // + operator overloading<BR> Time operator+ (const Time & t) const;<BR> Time operator- (const Time & t) const;<BR> Time operator* (double n) const;<BR> void Show () const;<BR> friend Time operator* (double m, const Time & t )<BR> {return t * m;} // inline definition<BR> friend void operator<< (ostream & os, const Time & t);<BR>};<BR>#endif<BR><BR><BR><BR><BR>// mytime0.cpp -- implement Time methods<BR>#include "mytime0.h"<BR><BR>Time::Time()<BR>{<BR> hours = minutes = 0;<BR>}<BR><BR>Time::Time(int h, int m )<BR>{<BR> hours = h;<BR> minutes = m;<BR>}<BR><BR>void Time::AddMin(int m )<BR>{<BR> minutes += m;<BR> hours += minutes / 60;<BR> minutes %= 60;<BR>}<BR><BR>void Time::AddHr(int h )<BR>{<BR> hours += h;<BR>}<BR><BR>void Time::Reset(int h, int m )<BR>{<BR> hours = h;<BR> minutes = m;<BR>}<BR><BR>Time Time::operator+ (const Time & t ) const<BR>{<BR> Time sum;<BR> sum.minutes = minutes + t.minutes;<BR> sum.hours = hours + t.hours + sum.minutes / 60;<BR> sum.minutes %= 60;<BR> return sum;<BR>}<BR><BR>Time Time::operator- (const Time & t ) const<BR>{<BR> Time diff;<BR> int tot1,tot2;<BR> tot1 = t.minutes + 60 * t.hours;<BR> tot2 = minutes + 60 * hours;<BR> diff.minutes = (tot2 - tot1) % 60;<BR> diff.hours = (tot2 - tot1) / 60;<BR> return diff;<BR>}<BR><BR>Time Time::operator* (double mult) const<BR>{<BR> Time result;<BR> long totalminutes = hours * mult * 60 + minutes*mult;<BR> result.hours = totalminutes / 60;<BR> result.minutes = totalminutes % 60;<BR> return result;<BR>}<BR>/*<BR>Time operator* (double mult, const Time & t )<BR>{<BR> Time result;<BR> long totalminutes = t.hours * mult * 60 + t.minutes * mult;<BR> result.hours = totalminutes / 60;<BR> result.minutes = totalminutes % 60;<BR> return result;<BR>}<BR>*/<BR>void Time::Show () const<BR>{<BR> cout << hours << " hours, " << minutes << " minutes ";<BR> cout << endl;<BR>}<BR><BR>void operator<< (ostream & os, const Time & t)<BR>{<BR> os << t.hours << " hours, " << t.minutes << " minutes";<BR>}<BR><BR><BR><BR><BR><BR>// usetime0.cpp -- use first draft of Time class<BR>// compile usetime0.cpp amd mytime0.cpp together<BR>#include <iostream><BR>#include "mytime0.h"<BR>using namespace std;<BR><BR>int main()<BR>{<BR> Time A;<BR> Time B(5,40);<BR> Time C(2,55);<BR><BR> cout << "A = ";<BR> A.Show ();<BR> cout << "B = ";<BR> B.Show();<BR> cout << "C = ";<BR> C.Show();<BR><BR> A = B.operator + (C);<BR> //A = B + C; // operator+ ()<BR> cout << "B + C = ";<BR> A.Show();<BR> A = B - C; // operator- ()<BR> cout << "B - C = ";<BR> A.Show();<BR> A = B * 2.75; // operator* ()<BR> cout << "B * 2.75 = ";<BR> A.Show();<BR><BR> A = 2.75 * B; // operator* ()<BR> cout << "2.75 * B = ";<BR> A.Show();<BR> return 0;<BR>}
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:Cxt_ann 回复日期:2003-12-19 15:31:56
<br>内容:呵呵,是我。<BR>你把头文件:#include <iostream><BR> using namespace std;<BR>改成:#include <iostream.h><BR>就没问题了,这是VC的问题,MS对标准支持的不好。<BR>:)
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:everfly 回复日期:2003-12-19 17:06:45
<br>内容:using namespace std;这个我开始没有去掉,所以出现错误了。呵呵,谢谢指点。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:Cxt_ann 回复日期:2003-12-19 17:29:11
<br>内容:呵呵,这是VC6.0的问题,它对标准C++支持的不好
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -