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

📄 subject_28896.htm

📁 一些关于vc的问答
💻 HTM
字号:
<p>
序号:28896 发表者:过台风 发表日期:2003-01-25 18:20:51
<br>主题:帮我看看
<br>内容:#include &lt;string.h&gt;<BR>#include &lt;iostream.h&gt;<BR><BR>class STRING {<BR>&nbsp;&nbsp;&nbsp;&nbsp;char *str;<BR>public:<BR>&nbsp;&nbsp;&nbsp;&nbsp;int strlen()const;<BR>&nbsp;&nbsp;&nbsp;&nbsp;int strcmp(const STRING &amp;) const;<BR>&nbsp;&nbsp;&nbsp;&nbsp;STRING &amp;strcpy(const STRING &amp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;STRING &amp;strcat(const STRING &amp;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;STRING(char *);<BR>&nbsp;&nbsp;&nbsp;&nbsp;~STRING();<BR>};<BR><BR>int STRING::strlen()const<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;return ::strlen(str);<BR>}<BR><BR>int STRING::strcmp(const STRING &amp;string)const<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;return ::strcmp(str,string.str);<BR>}<BR><BR>STRING &amp;STRING::strcpy(const STRING &amp;string)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;::strcpy(str,string.str);<BR>&nbsp;&nbsp;&nbsp;&nbsp;return *this;<BR>}<BR><BR>STRING &amp;STRING::strcat(const STRING &amp;string)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;::strcat(str,string.str);<BR>&nbsp;&nbsp;&nbsp;&nbsp;return *this;<BR>}<BR><BR>STRING::STRING(char *t)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;str=new char[::strlen(t)+1];<BR>&nbsp;&nbsp;&nbsp;&nbsp;::strcpy(str,t);<BR>}<BR><BR>STRING::~STRING()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;if (str) delete str;str=0;<BR>}<BR><BR>void main(void)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;STRING s1("i like apple");<BR>&nbsp;&nbsp;&nbsp;&nbsp;STRING s2(" and pear");<BR>&nbsp;&nbsp;&nbsp;&nbsp;STRING s3(" and orange");<BR>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;"Length of s1="&lt;&lt;s1.strlen()&lt;&lt;endl;<BR>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;"Length of s2="&lt;&lt;s2.strlen()&lt;&lt;endl;<BR>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;"Length of s3="&lt;&lt;s3.strlen()&lt;&lt;endl;<BR>&nbsp;&nbsp;&nbsp;&nbsp;s1.strcat(s2).strcat(s3);<BR>}<BR><BR>编译通过 运行中断<BR>估计是STRING &amp;STRING::strcpy(const STRING &amp;string)<BR>除了问题
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:upstream 回复日期:2003-01-25 19:18:29
<br>内容:错误是在析构函数中出现,确切的说是在析构s1的时候出错&nbsp;&nbsp; <BR>if (str) delete str;str=0;出错<BR><BR><BR><BR>原来s1.str指向i like apple;<BR>经过s1.strcat(s2).strcat(s3);后s1.str改变了指向,<BR>造成delete str出错。<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>回复者:tjhe 回复日期:2003-01-25 20:12:14
<br>内容:strcat中有问题,因为str的大小没进行改变,执行strcat将产生非法操作。<BR><BR>STRING &amp;STRING::strcat(const STRING &amp;string)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;int len = ::strlen(str)+::strlen(string.str)+1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;char* temp = str;<BR>&nbsp;&nbsp;&nbsp;&nbsp;str = new char[len];<BR>&nbsp;&nbsp;&nbsp;&nbsp;::strcpy(str,temp);<BR>&nbsp;&nbsp;&nbsp;&nbsp;::strcat(str,string.str);<BR>&nbsp;&nbsp;&nbsp;&nbsp;delete[] temp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;return *this;<BR>}<BR><BR>另外析构函数中<BR>STRING::~STRING()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;if (str) delete[] str;str=0;&nbsp;&nbsp;//应使用delete[]<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>
回复者:lilei 回复日期:2003-01-30 20:22:50
<br>内容:我想问一下,是否起码要看得懂上面的程序才可以进行WINDOWS SOCKET编程呢??<BR>请指教!!
<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 + -