为什么这个程序运行出错呀,谢谢各位,我没发现什么错误呀
#include <iostream.h>#include <string.h>
class MyString
{
public:
MyString() {m_data = NULL;}
MyString(const char *str);
~MyString();
MyString operator-(const MyString &other);
MyString operator=(const MyString &other);
friend ostream &operator<<(ostream &stream, const MyString &str);
//重载输出运算符"<<"
private:
char *m_data;
};
#define NULL 0
MyString::~MyString()
{
if(m_data)
delete [] m_data;
m_data = NULL;
}
MyString::MyString(const char *str)
{
if(str == NULL)
{
m_data = new char[1];
*m_data = '\0';
}
else
{
int length = strlen(str);
m_data = new char[length+1];
strcpy(m_data, str);
}
}
MyString MyString::operator=(const MyString &other)
{
if(this==&other)
return *this;
delete m_data;
m_data=new char[strlen(other.m_data)+1];
strcpy(m_data,other.m_data);
return *this;
}
MyString MyString::operator-(const MyString &other)
{ int i,j,t=0,m;
MyString temp;
int length1 = strlen(other.m_data);
int length2 = strlen(m_data);
for(;t<length2;t++)
{
i=t;j=0;
while(*(m_data+i)!='\0'&&*(other.m_data+j)!='\0')
if(m_data[i]==other.m_data[j])
{i++;j++;}
else{
i=i-j+1;
j=0;
}
if(*(other.m_data+j)=='\0')
{m=i-j;
while(*(m_data+m)!='\0')
{
*(m_data+m)=*(m_data+m+length1);
m++;
}
}
}
temp.m_data=new char[strlen(m_data)+1];
strcpy(temp.m_data,m_data);
return temp;
}
ostream& operator<<(ostream &stream, const MyString &str)
{
stream<<str.m_data;
return stream;
}
void main()
{
MyString str1("abcdefgabef"), str2("ef");
MyString str3;
cout<<"str1->"<<str1<<endl;
cout<<"str2->"<<str2<<endl;
str3 =str1-str2;
cout <<"str3=str1-str2,str3->"<<str3<<endl;
}