当我尝试执行时程序崩溃请告诉我以下代码中的问题。
该程序即将连接字符串,并在运算符重载的帮助下找到更大的字符串
我使用了复制构造函数和运算符重载的概念
有字符串操作的库string.h
#include<iostream>
#include<string.h>
#include<conio.h>
using namespace std;
class _string
{
char *p;
int len;
public:
_string()
{
p=NULL;
len=0;
}
_string(const char *s)
{
len = strlen(s);
p = new char[len + 1];
strcpy(p, s);
}
void getdata()
{
char *str;
cout << "enter string" << endl;
cin >> str;
len = strlen(str);
p = new char[len + 1];
strcpy(p, str);
}
_string operator+(_string s)
{
_string temp;
temp.len = len + s.len;
temp.p = new char[temp.len + 1];
strcpy(temp.p, s.p);
strcat(temp.p, p);
return temp;
}
int operator>(_string s)
{
if(strlen(p) > strlen(s.p))
return 1;
else
return 0;
}
void showdata(void)
{
cout << p << " string is bigger" << endl;
}
~_string()
{
delete p;
}
};
int main()
{
_string s1, s2, s3("new ");
s1.getdata();
s2.getdata();
_string t1, t2;
t1 = s1 + s3;
t2 = s2 + s3;
if (t1 > t2)
t1.showdata();
else
t2.showdata();
getch();
return 0;
}