📄 subject_31817.htm
字号:
<p>
序号:31817 发表者:芝麻不开门 发表日期:2003-03-07 02:39:09
<br>主题:重载了一个cout<<运算符,可是出现错误。
<br>内容:#include <iostream.h><BR>class Orbit<BR>{<BR>public:<BR> Orbits () {x[0] = x[1] = 0;}<BR> Orbits (double t, double p) {x[0] = t, x[1] = p;}<BR> Orbits (const Orbits &next) {*this = next;} <BR> <BR> friend ostream& operator << (ostream& cout, Orbits a);<BR>private:<BR> double x[2];<BR>};<BR><BR>ostream& operator << (ostream& cout, Orbits a)<BR>{<BR> cout << "(" << a.x[0] << "\t " << a.x[1] << ")";<BR> return cout;<BR>}<BR><BR>debug:<BR>error C2872: 'ostream' : ambiguous symbol<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>
回复者:未出手的飞刀 回复日期:2003-03-07 09:29:15
<br>内容:#include <iostream.h><BR>class Orbit<BR>{<BR>public:<BR> Orbits () {x[0] = x[1] = 0;}<BR> Orbits (double t, double p) {x[0] = t, x[1] = p;}<BR> Orbits (const Orbits &next) {*this = next;} <BR> <BR>friend std::ostream& operator << (std::ostream& cout, Orbits a);<BR>private:<BR> double x[2];<BR>};<BR><BR>std::ostream& operator << (std::ostream& cout, Orbits a)<BR>{<BR> cout << "(" << a.x[0] << "\t " << a.x[1] << ")";<BR> return cout;<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-03-07 11:08:54
<br>内容:请注意: 你的 Orbits 的声名错误! 没加s!!!!!<BR><BR>#include <iostream.h><BR>class Orbits<BR>{<BR>public:<BR> Orbits () {x[0] = x[1] = 0;}<BR> Orbits (double t, double p) {x[0] = t, x[1] = p;}<BR> Orbits (const Orbits &next) {*this = next;} <BR> <BR> friend ostream& operator << (ostream& cout, Orbits& a);<BR><BR>private:<BR> double x[2];<BR>};<BR><BR>ostream& operator << (ostream& cout, Orbits& a)<BR>{<BR> cout << "(" << a.x[0] << "\t " << a.x[1] << ")";<BR> return cout;<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>
回复者:tjhe 回复日期:2003-03-07 16:53:23
<br>内容:不应该说是重载cout<<运算符,是重载流插入运算符<<,cout已经是被定义为标准输出对象了,为了避免混淆,建议将<BR>friend ostream& operator << (ostream& cout, Orbits a);<BR>改为<BR>friend ostream& operator << (ostream& out, Orbits a);<BR>更具一般性<BR><BR>#include <iostream.h><BR>class Orbits<BR>{<BR>public:<BR> Orbits () {x[0] = x[1] = 0;}<BR> Orbits (double t, double p) {x[0] = t, x[1] = p;}<BR> Orbits (const Orbits &next) {*this = next;} <BR> <BR>friend ostream& operator << (ostream& out, Orbits a);<BR>private:<BR> double x[2];<BR>};<BR><BR>ostream& operator << (ostream& out, Orbits a)<BR>{<BR> out << "(" << a.x[0] << "\t " << a.x[1] << ")";<BR> return out;<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-03-11 03:18:55
<br>内容:to:第一楼飞刀:<BR>按你的程序编译有错误!<BR>c:\program files\microsoft visual studio\common\msdev98\bin\test.cpp(9) : error C2653: 'std' : is not a class or namespace name<BR>to:第二楼陈旭:<BR>这个是俺的笔误,应为Orbits.<BR>to:ALL<BR>>>><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>
回复者:微笑的撒旦 回复日期:2003-03-11 12:00:49
<br>内容:把#include <iostream.h><BR>换成#include <iostream><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>回复者:未出手的飞刀 回复日期:2003-03-12 23:39:42
<br>内容:<BR>应该把#include <iostream.h><BR>换成#include <iostream>就行。<BR>或把using namespace std;去掉。<BR>前一办法更好,尽量用无.h的头文件<BR><BR>因<iostream.h>中没namespace std的声明,只有<iostream>有。<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-03-13 09:07:45
<br>内容:如果加上 using namespace std ,<BR>错误会更加严重! 因为此时的friend已经失效!!!!<BR>也不知道为什么,我经常会碰到这种错误::<BR> <BR> !!!!!!!!不能访问私有数据!!!!!!!!!!<BR><BR>如果非要用friend,就只能添加Get& 函数,在作输出;<BR>如此以来就非常麻烦,所以还是不用 namespace 好!
<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 + -