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

📄 subject_34276.htm

📁 vc
💻 HTM
字号:
<p>
序号:34276 发表者:rayjoe 发表日期:2003-03-28 10:30:57
<br>主题:请教一个编译错误,清高手指点。
<br>内容:我用的编译器是VC 6.0,代码如下:<BR><BR> const ostream&amp; operator&lt;&lt;(ostream &amp;os, String const &amp;s)<BR> {<BR>&nbsp;&nbsp;&nbsp;&nbsp; return os&lt;&lt;s.getstr();<BR> }<BR> <BR> 其中s是自己定义的一个class, getstr()是它的public成员函数。<BR><BR> 错误是:<BR>&nbsp;&nbsp;error C2662: 'getstr' : cannot convert 'this' pointer from 'const class String' to 'class String &amp;'<BR><BR>&nbsp;&nbsp;不知道如何解决?<BR><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:dongfanghong 回复日期:2003-03-28 10:55:00
<br>内容:const ostream&amp; operator&lt;&lt;(ostream &amp;os, String const s)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp; return os&lt;&lt;s.getstr();<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>
回复者:rayjoe 回复日期:2003-03-28 11:15:20
<br>内容:还是错,就是变了点: <BR><BR>error C2662: 'getstr' : cannot convert 'this' pointer from 'const class String' to 'class String &amp;'
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:mimibaby 回复日期:2003-03-28 11:38:06
<br>内容:换个涵数名,别用getstr!
<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-28 11:47:19
<br>内容:不要用return语句返回指向“栈内存”的指针,因为该内存在函数结束时自动消亡
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:rayjoe 回复日期:2003-03-28 14:31:36
<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>
回复者:dongfanghong 回复日期:2003-03-28 15:30:47
<br>内容:把String 的定义传上来看看。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:bbc 回复日期:2003-03-28 20:10:12
<br>内容:const ostream&amp; operator&lt;&lt;(ostream &amp;os, String &amp;s)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp; return os&lt;&lt;s.getstr();<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>
回复者:rayjoe 回复日期:2003-03-29 09:15:24
<br>内容://下面是头文件的全部内容<BR>#include &lt;iostream.h&gt;<BR><BR>class String;<BR><BR>istream&amp; operator&gt;&gt;(istream&amp;, String&amp;);<BR>ostream&amp; operator&lt;&lt;(ostream&amp;, const String&amp;);<BR><BR>class String<BR>{<BR>public:<BR>&nbsp;&nbsp;&nbsp;&nbsp;String();<BR>&nbsp;&nbsp;&nbsp;&nbsp;String( const char*);<BR>&nbsp;&nbsp;&nbsp;&nbsp;String(const String&amp;);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;~String();<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String&amp; operator=(const String&amp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;String&amp; operator=(const char*);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;bool operator==(const String&amp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;bool operator==(const char*);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;char&amp; operator[](int);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;int size(){return _size;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;char* getstr(){return _string;}<BR><BR>private:<BR>&nbsp;&nbsp;&nbsp;&nbsp;int _size;<BR>&nbsp;&nbsp;&nbsp;&nbsp;char* _string;<BR>};&nbsp;&nbsp;&nbsp;&nbsp;
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:rayjoe 回复日期:2003-03-29 16:35:09
<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>
回复者:max cai 回复日期:2003-03-29 21:35:49
<br>内容:This error may be caused by invoking a non-const member function on a const object. To correct the problem, remove the const from the object declaration or add const to the member function.<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>
回复者:dongfanghong 回复日期:2003-03-31 09:09:03
<br>内容:这样写:<BR>const ostream&amp; operator&lt;&lt;(ostream &amp;os, String &amp;s)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp; return os&lt;&lt;s.getstr();<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>
回复者:rayjoe 回复日期:2003-04-01 09:12:59
<br>内容:哎,我都不好意思,这么多人给我替建议,想办法,可是就是搞不定,<BR>东方红兄弟的这招也没能解决问题,这会出现了连接错误:<BR><BR>error LNK2001: unresolved external symbol "class ostream &amp; __cdecl <BR>operator&lt;&lt;(class ostream &amp;,class String const &amp;)" (??6@YAAAVostream@@AAV0@ABVString@@@Z)
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:dongfanghong 回复日期:2003-04-01 09:23:51
<br>内容:声明的地方CONST也要去掉啊:<BR><BR>//下面是头文件的全部内容<BR>#include &lt;iostream.h&gt;<BR><BR>class String;<BR><BR>istream&amp; operator&gt;&gt;(istream&amp;, String&amp;);<BR>ostream&amp; operator&lt;&lt;(ostream&amp;, String&amp;);
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:rayjoe 回复日期:2003-04-01 09:48:01
<br>内容:error C2556: <BR>'const class ostream &amp;__cdecl operator &lt;&lt;(class ostream &amp;,class String &amp;)' <BR>: overloaded function differs only by return type from <BR>'class ostream &amp;__cdecl operator &lt;&lt;(class ostream &amp;,class String &amp;)'<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;const ostream&amp; operator&lt;&lt;(ostream &amp;os, String &amp;s)<BR>--〉{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return os&lt;&lt;s.getstr();<BR>&nbsp;&nbsp;&nbsp;&nbsp;} <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>回复者:dongfanghong 回复日期:2003-04-01 11:06:25
<br>内容:目前错在声明和实现写法不一样。改正一样就行了。<BR>声明:<BR>ostream&amp; operator&lt;&lt;(ostream&amp;, String&amp;); <BR>实现:<BR>ostream&amp; operator&lt;&lt;(ostream &amp;os, String &amp;s)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp; return os&lt;&lt;s.getstr();<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>
回复者:rayjoe 回复日期:2003-04-01 21:31:16
<br>内容:哇赛,果然解决了,真是不容易<BR><BR>但是如果两个都写成这样,就不行了:<BR>声明:<BR>ostream&amp; operator&lt;&lt;(ostream&amp;, const String&amp;); <BR>实现:<BR>ostream&amp; operator&lt;&lt;(ostream &amp;os, const String &amp;s)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp; return os&lt;&lt;s.getstr();<BR>}<BR><BR>呵呵,说不能把this指针从const string转成 string。<BR><BR>thanks a lot,"east red",总算搞出个结果。 
<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 + -