myString operator+(const myString &s1,const myString &s2)
{
int num,i,j;
num=s1.len+s2.len;
myString s3;
s3.len=num;
s3.start=new char [num];
strcpy(s3.start, s1.start);
strcat(s3.start, s2.start);
return s3;
}
为什么return s3时会出错?
{
int num,i,j;
num=s1.len+s2.len;
myString s3;
s3.len=num;
s3.start=new char [num];
strcpy(s3.start, s1.start);
strcat(s3.start, s2.start);
return s3;
}
为什么return s3时会出错?
解决方案
5
估计是你复制时越界了,num有没有考虑最后的\0字符
10
很明显越界,字符串最后都有一个‘\0″
50
你错误的地方在构造函数里,越界了,破坏了堆空间,导致析构的时候出错
start=new char [strlen(x)];改为start=new char [strlen(x)+1];
start=new char [strlen(x)];改为start=new char [strlen(x)+1];
myString::myString(const char *x) { start=new char [strlen(x)+1]; int i; for(i=0;i<strlen(x);i++) { start[i]=x[i]; } start[i]="\0"; }