📄 subject_28896.htm
字号:
<p>
序号:28896 发表者:过台风 发表日期:2003-01-25 18:20:51
<br>主题:帮我看看
<br>内容:#include <string.h><BR>#include <iostream.h><BR><BR>class STRING {<BR> char *str;<BR>public:<BR> int strlen()const;<BR> int strcmp(const STRING &) const;<BR> STRING &strcpy(const STRING &);<BR> STRING &strcat(const STRING &);<BR> STRING(char *);<BR> ~STRING();<BR>};<BR><BR>int STRING::strlen()const<BR>{<BR> return ::strlen(str);<BR>}<BR><BR>int STRING::strcmp(const STRING &string)const<BR>{<BR> return ::strcmp(str,string.str);<BR>}<BR><BR>STRING &STRING::strcpy(const STRING &string)<BR>{<BR> ::strcpy(str,string.str);<BR> return *this;<BR>}<BR><BR>STRING &STRING::strcat(const STRING &string)<BR>{<BR> ::strcat(str,string.str);<BR> return *this;<BR>}<BR><BR>STRING::STRING(char *t)<BR>{<BR> str=new char[::strlen(t)+1];<BR> ::strcpy(str,t);<BR>}<BR><BR>STRING::~STRING()<BR>{<BR> if (str) delete str;str=0;<BR>}<BR><BR>void main(void)<BR>{<BR> STRING s1("i like apple");<BR> STRING s2(" and pear");<BR> STRING s3(" and orange");<BR> cout<<"Length of s1="<<s1.strlen()<<endl;<BR> cout<<"Length of s2="<<s2.strlen()<<endl;<BR> cout<<"Length of s3="<<s3.strlen()<<endl;<BR> s1.strcat(s2).strcat(s3);<BR>}<BR><BR>编译通过 运行中断<BR>估计是STRING &STRING::strcpy(const STRING &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的时候出错 <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 &STRING::strcat(const STRING &string)<BR>{<BR> int len = ::strlen(str)+::strlen(string.str)+1;<BR> char* temp = str;<BR> str = new char[len];<BR> ::strcpy(str,temp);<BR> ::strcat(str,string.str);<BR> delete[] temp;<BR> return *this;<BR>}<BR><BR>另外析构函数中<BR>STRING::~STRING()<BR>{<BR> if (str) delete[] str;str=0; //应使用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 + -