输出语句触发断点怎么破?
这个程序运行有时候正常运行,有时候就意外停止,意外停止的时候最后一行输出就输出不出来,我调试的时候也是,有时候正常,有时候在最后一行的输出语句那里触发断点。为什么输出语句会出现问题呢?
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string.h>
using namespace std;
class mystring
{
private:
char *str;
public:
mystring(char*a = "0")
{
str = new char[100];
strcpy(str, a);
}
friend mystring &operator+(mystring &s1, mystring &s2);
int operator>(mystring &s);
void show()
{
cout << str << endl;
}
};
mystring &operator+(mystring &s1, mystring &s2)
{
char*b = new char(strlen(s1.str) + 1);
strcpy(b, s1.str);
strcat(s1.str, s2.str);
mystring a;
strcpy(a.str, s1.str);
strcpy(s1.str, b);
return a;
}
int mystring::operator>(mystring &s)
{
if (strcmp(str, s.str)) return 1;
else return 0;
}
int main(void)
{
char in1[50], in2[50];
cin.getline(in1, 50);
cin.getline(in2, 50);
mystring s1(in1), s2(in2);
mystring s3;
s3 = s1 + s2;
s1.show();
s2.show();
s3.show();
int z = (s1 > s2);
cout << z <<'\n';
return 0;
}