⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 subject_58494.htm

📁 vc
💻 HTM
字号:
<p>
序号:58494 发表者:0000000 发表日期:2003-11-01 14:49:31
<br>主题:帮我看看
<br>内容:#include&lt;iostream&gt;<BR>#include&lt;sstream&gt;<BR>using namespace std;<BR>class Point<BR>{friend <BR>ostream&amp; operator&lt;&lt;(ostream&amp; ,const Point&amp; );<BR>public:<BR>Point(int x=0.0,int y=0.0):_x(x),_y(y){}<BR>Point(const Point&amp; p):_x(p._x),_y(p._y){}<BR>~Point(){}<BR>Point&amp; operator=(const Point&amp;p)<BR>{_x=p._x;<BR>return *this;<BR>}<BR>double x() const{return _x;}<BR>double y() const{return _y;}<BR>string toString() const<BR>{ostringstream output;<BR>output&lt;&lt;&#34;(&#34;&lt;&lt;_x&lt;&lt;&#34;,&#34;&lt;&lt;_y&lt;&lt;&#34;)&#34;;<BR>return output.str();<BR>}<BR>protected:<BR>&nbsp;&nbsp;&nbsp;&nbsp;int _x,_y;<BR>};<BR> ostream&amp; operator&lt;&lt;(ostream&amp; ostr,const Point&amp; point)<BR>{return ostr&lt;&lt;point.toString ();}<BR>int main()<BR>{Point p(2,4);<BR>cout&lt;&lt;p;<BR>}<BR><BR><BR>--------------------Configuration: Cpp1 - Win32 Debug--------------------<BR>Compiling...<BR>Cpp1.cpp<BR>D:\vcex\Cpp1.cpp(29) : error C2593: 'operator &lt;&lt;' is ambiguous<BR>D:\vcex\Cpp1.cpp(30) : warning C4508: 'main' : function should return a value; 'void' return type assumed<BR>Error executing cl.exe.<BR><BR>Cpp1.exe - 1 error(s), 1 warning(s)<BR>为什么将“ostream&amp; operator&lt;&lt;(ostream&amp; ostr,const Point&amp; point)<BR>{return ostr&lt;&lt;point.toString ();}”放在类里才不会出错,对多载不是很明白,哪位大虾指点一下,谢谢<BR>
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:红苹果 回复日期:2003-11-01 22:27:53
<br>内容:代码本身没错,应该是在命名空间(namespace)的使用中出了什么问题。系统没有把类中的friend函数原型与类外的函数定义看成是一个函数(具体原因我也没搞懂)。若把:<BR>#include&lt;iostream&gt;<BR>#include&lt;sstream&gt;<BR>using namespace std;<BR>换成:<BR>#include&lt;iostream.h&gt;<BR>#include&lt;sstream.h&gt;<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>
回复者:saka 回复日期:2003-11-02 00:40:15
<br>内容:#include&lt;iostream&gt;<BR>#include&lt;sstream&gt;<BR>using namespace std;<BR>class Point<BR>{friend <BR>ostream&amp; operator&lt;&lt;(ostream&amp; ,const Point&amp; );<BR>public:<BR>Point(int x=0.0,int y=0.0):_x(x),_y(y){}<BR>Point(const Point&amp; p):_x(p._x),_y(p._y){}<BR>~Point(){}<BR>Point&amp; operator=(const Point&amp;p)<BR>{_x=p._x;<BR>return *this;<BR>}<BR>double x() const{return _x;}<BR>double y() const{return _y;}<BR>string toString() const<BR>{ostringstream output;<BR>output&lt;&lt;&#34;(&#34;&lt;&lt;_x&lt;&lt;&#34;,&#34;&lt;&lt;_y&lt;&lt;&#34;)&#34;;<BR>return output.str();<BR>}<BR>protected:<BR>&nbsp;&nbsp;&nbsp;&nbsp;int _x,_y;<BR>};<BR>ostream&amp; operator&lt;&lt;(ostream&amp; ostr,const Point&amp; point)<BR>{return ostr&lt;&lt;point.toString ();}<BR>int main()<BR>{Point p(2,4);<BR>cout&lt;&lt;p;<BR>}<BR><BR>friend <BR>ostream&amp; operator&lt;&lt;(ostream&amp; ,const Point&amp; );这句不要<BR>main()中加入return -1;<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-11-02 00:49:48
<br>内容:2楼的:<BR>问题是当要在operator&lt;&lt;( )函数中直接访问Point类的私有成员时怎么办?没解决呀
<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 + -