在我程序中用了delete[]后,通过调试后程序就不知道跑哪里了
在运行delete[]这一步,然后调试下一步的时候,程序就响了一下,然后— —
我就不知道程序跑到哪里去了,上网查也没查出什么
拜托
源码:
#include
#include
#include
#include
#include
using namespace std;
class Stu
{
public:
char *name;
int age;
int num;
char sex;
Stu(char *name = "", int age = 0, int num = 0, char sex = ' ');
~Stu();
friend ostream &operator<< (ostream &out, Stu &A);
friend istream &operator>> (istream &in, Stu &A);
};
Stu::Stu(char *name, int age, int num, char sex)
{
this->name = new char[strlen(name + 1)];
strcpy(this->name, name);
this->age = age;
this->num = num;
this->sex = sex;
}
Stu::~Stu()
{
delete[] name;
cout << "OK!" << endl;
}
ostream &operator<< (ostream &out, Stu &A)
{
out << A.name << " " << A.age << " " << A.num << " " << A.sex;
return out;
}
istream &operator>> (istream &in, Stu &A)
{
char tmp[10];
cout << "姓名:";
in >> tmp;
delete[] A.name;
A.name = new char[strlen(tmp + 1)];
strcpy(A.name, tmp);
cout << "年龄:";
in >> A.age;
cout << "学号:";
in >> A.num;
cout << "性别:";
in >> A.sex;
return in;
}
void Add()
{
Stu A;
cin >> A;
fstream p;
p.open("学生信息.txt", ios::ate | ios::out);
p << A;
p.close();
}
int main()
{
while (1)
{
Add();
system("pause");
}
return 0;
}