📄 subject_58494.htm
字号:
<p>
序号:58494 发表者:0000000 发表日期:2003-11-01 14:49:31
<br>主题:帮我看看
<br>内容:#include<iostream><BR>#include<sstream><BR>using namespace std;<BR>class Point<BR>{friend <BR>ostream& operator<<(ostream& ,const Point& );<BR>public:<BR>Point(int x=0.0,int y=0.0):_x(x),_y(y){}<BR>Point(const Point& p):_x(p._x),_y(p._y){}<BR>~Point(){}<BR>Point& operator=(const Point&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<<"("<<_x<<","<<_y<<")";<BR>return output.str();<BR>}<BR>protected:<BR> int _x,_y;<BR>};<BR> ostream& operator<<(ostream& ostr,const Point& point)<BR>{return ostr<<point.toString ();}<BR>int main()<BR>{Point p(2,4);<BR>cout<<p;<BR>}<BR><BR><BR>--------------------Configuration: Cpp1 - Win32 Debug--------------------<BR>Compiling...<BR>Cpp1.cpp<BR>D:\vcex\Cpp1.cpp(29) : error C2593: 'operator <<' 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& operator<<(ostream& ostr,const Point& point)<BR>{return ostr<<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<iostream><BR>#include<sstream><BR>using namespace std;<BR>换成:<BR>#include<iostream.h><BR>#include<sstream.h><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<iostream><BR>#include<sstream><BR>using namespace std;<BR>class Point<BR>{friend <BR>ostream& operator<<(ostream& ,const Point& );<BR>public:<BR>Point(int x=0.0,int y=0.0):_x(x),_y(y){}<BR>Point(const Point& p):_x(p._x),_y(p._y){}<BR>~Point(){}<BR>Point& operator=(const Point&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<<"("<<_x<<","<<_y<<")";<BR>return output.str();<BR>}<BR>protected:<BR> int _x,_y;<BR>};<BR>ostream& operator<<(ostream& ostr,const Point& point)<BR>{return ostr<<point.toString ();}<BR>int main()<BR>{Point p(2,4);<BR>cout<<p;<BR>}<BR><BR>friend <BR>ostream& operator<<(ostream& ,const Point& );这句不要<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<<( )函数中直接访问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 + -