📄 subject_52475.htm
字号:
<p>
序号:52475 发表者:提刀入人间 发表日期:2003-09-11 11:27:45
<br>主题:操作符重载出错,兄弟门帮帮忙!c2679错误!
<br>内容:#include "mytime0.h"<BR><BR>time:: time ()<BR>{<BR> hours = minutes = 0 ;<BR>}<BR>time :: time (int h , int m)<BR>{<BR> hours = h;<BR> minutes = m;<BR>}<BR>void time :: addmin (int m)<BR>{<BR> minutes += m;<BR> hours += minutes /60;<BR> minutes %= 60;<BR>}<BR>void time :: addhr (int h)<BR>{<BR> hours = h;<BR>}<BR>void time :: reset (int h,int m)<BR>{<BR> hours = h;<BR> minutes = m;<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>void time ::show () const<BR>{<BR> cout << hours << "hours "<BR> << minutes << "minutes ";<BR> cout << "\n";<BR>}<BR>#include <iostream><BR>#include "mytime0.h"<BR>using namespace std;<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><BR>这一行: a = b . operator + ;<BR> cout << "a = b . operator + =";<BR> a . show ();<BR> b=a +c;<BR> cout << "a +c=";<BR> b. show ();<BR> return 0;<BR>}<BR>g:\save\my ebooks\usetime.cpp(18) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class time (__thiscall time::*)(const class time &) const' (or there is no acceptable conversion)<BR>
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:提刀入人间 回复日期:2003-09-11 11:28:52
<br>内容:MSDN的解释,看不懂!<BR>Compiler Error C2679<BR>binary 'operator' : no operator defined which takes a right-hand operand of type 'type' (or there is no acceptable conversion)<BR><BR>Define a constructor or conversion operator to make the required conversion. If you’ve encountered this error on code which compiled with an earlier version of Visual C++, please read Technote: Improved Conformance to ANSI C++ for more information.<BR><BR>The following is an example of this error:<BR><BR>class C<BR>{<BR> public:<BR> C(); // no constructor with an int argument<BR>} c;<BR>class D<BR>{<BR> public:<BR> D( int );<BR> D();<BR>} d;<BR>void main()<BR>{<BR> c = 10; // error<BR> d = 10; // OK<BR>}<BR><BR><BR>--------------------------------------------------------------------------------<BR>Send feedback to MSDN.Look here for MSDN Online resources.
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:ShaGua 回复日期:2003-09-11 11:41:59
<br>内容:应该说的是返回的数据问题。<BR>time不能实现转换。<BR><BR>time time :: operator + (const time & t ) const<BR><BR>要求建立一个构造函数或者转换操作。<BR><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>
回复者:提刀入人间 回复日期:2003-09-11 11:44:44
<br>内容:还是不明白!<BR>说详细点!!<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>回复者:ShaGua 回复日期:2003-09-11 12:01:06
<br>内容:增加一个构造函数:<BR>time (time &t);<BR>在b=a+c;时候返回结果时候要用到。<BR><BR>下面是一个例子,不是很完善:<BR>#include <iostream><BR>//#include "mytime0.h"<BR>using namespace std;<BR><BR><BR>class time <BR>{<BR>public:<BR> time ();<BR> time (int h,int m); <BR> time (time &t);<BR><BR> void addmin (int m);<BR> void addhr (int h);<BR> void reset (int h,int m);<BR> void show () const;<BR> <BR>// time& operator = (const time & t ) ;<BR> time operator + (const time & t ) ;<BR><BR>protected:<BR> int hours;<BR> int minutes;<BR>};<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>time ::time(time &t)<BR>{<BR> minutes = t. minutes;<BR> hours = t.hours+minutes /60;<BR> minutes %= 60;<BR>}<BR><BR>void time :: addmin (int m)<BR>{<BR> minutes += m;<BR> hours += minutes /60;<BR> minutes %= 60;<BR>}<BR>void time :: addhr (int h)<BR>{<BR> hours = h;<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 ) <BR>{ <BR> minutes += t. minutes;<BR> hours = hours + t. hours +minutes /60;<BR> minutes %= 60;<BR> return *this;<BR>}<BR><BR>void time ::show () const<BR>{<BR> cout << hours << "hours "<BR> << minutes << "minutes ";<BR> cout << "\n";<BR>}<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;<BR> cout << "a = b . operator + =";<BR> a . show ();<BR> b=a +c;<BR> cout << "a +c=";<BR> b. show ();<BR><BR> return 0;<BR>}<BR><BR>2003-9-11 13:10:28
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -