主题:析构函数带来的错误
各位高手,我的程序如下(在VC++6.0下),如果没有析构函数(红色文字)就能正常运行,如果加上析构函数,就不能正常运行。请问为什么?如果要用析构函数的话,应该怎么写代码。谢谢!
#include<iostream.h>
#include<string.h>
class Person
{
private:
char* name;
int age;
char* gender;
public:
Person(){}
Person(const char *name_t,const int age_t,const char *gender_t);
Person(Person& person);
void setName(const char *name_t);
void setAge(const int age_t);
void setGender(const char *gender_t);
char* getName();
int getAge();
char* getGender();
void display();
[color=FF0000]~Person()
{
delete[] name;
delete[] gender;
}[/color]
};
Person :: Person(const char *name_t,const int age_t,const char *gender_t)
{
name = new char[strlen(name_t) + 1];
strcpy(name,name_t);
gender = new char[strlen(gender_t) + 1];
strcpy(gender,gender_t);
age = age_t;
}
Person :: Person(Person& person)
{
name = new char[strlen(person.name) + 1];
strcpy(name,person.name);
gender = new char[strlen(person.gender) + 1];
strcpy(gender,person.gender);
age = person.age;
}
void Person::setName(const char* name_t)
{
name = new char[strlen(name_t) + 1];
strcpy(name,name_t);
}
void Person ::setAge(const age_t)
{
age = age_t;
}
void Person :: setGender(const char* gender_t)
{
gender = new char[strlen(gender_t) + 1];
strcpy(gender,gender_t);
}
char* Person :: getName()
{
return name;
}
int Person :: getAge()
{
return age;
}
char* Person :: getGender()
{
return gender;
}
void Person :: display()
{
cout << name << " | " << age << " | " << gender << endl;
}
//建立类模板Relation,该类模板生成的模板类由参数K、T决定。
template <class K,class T>
class Relation
{
private:
K key;
T value;
public:
Relation(){}
Relation(const K& k,const T& v)
{
key = k;
value = v;
}
K getKey()
{
return key;
}
T getValue()
{
return value;
}
void setKey(const K& k)
{
key = k;
}
void setValue(const T& v)
{
value = v;
}
};
//在主函数中测试
int main()
{
Relation<char* ,Person> r;
r.setKey("CEO");
Person person("张三",40,"男");
r.setValue(person);
cout << r.getKey() <<" : ";
r.getValue().display();
return 0;
}
#include<iostream.h>
#include<string.h>
class Person
{
private:
char* name;
int age;
char* gender;
public:
Person(){}
Person(const char *name_t,const int age_t,const char *gender_t);
Person(Person& person);
void setName(const char *name_t);
void setAge(const int age_t);
void setGender(const char *gender_t);
char* getName();
int getAge();
char* getGender();
void display();
[color=FF0000]~Person()
{
delete[] name;
delete[] gender;
}[/color]
};
Person :: Person(const char *name_t,const int age_t,const char *gender_t)
{
name = new char[strlen(name_t) + 1];
strcpy(name,name_t);
gender = new char[strlen(gender_t) + 1];
strcpy(gender,gender_t);
age = age_t;
}
Person :: Person(Person& person)
{
name = new char[strlen(person.name) + 1];
strcpy(name,person.name);
gender = new char[strlen(person.gender) + 1];
strcpy(gender,person.gender);
age = person.age;
}
void Person::setName(const char* name_t)
{
name = new char[strlen(name_t) + 1];
strcpy(name,name_t);
}
void Person ::setAge(const age_t)
{
age = age_t;
}
void Person :: setGender(const char* gender_t)
{
gender = new char[strlen(gender_t) + 1];
strcpy(gender,gender_t);
}
char* Person :: getName()
{
return name;
}
int Person :: getAge()
{
return age;
}
char* Person :: getGender()
{
return gender;
}
void Person :: display()
{
cout << name << " | " << age << " | " << gender << endl;
}
//建立类模板Relation,该类模板生成的模板类由参数K、T决定。
template <class K,class T>
class Relation
{
private:
K key;
T value;
public:
Relation(){}
Relation(const K& k,const T& v)
{
key = k;
value = v;
}
K getKey()
{
return key;
}
T getValue()
{
return value;
}
void setKey(const K& k)
{
key = k;
}
void setValue(const T& v)
{
value = v;
}
};
//在主函数中测试
int main()
{
Relation<char* ,Person> r;
r.setKey("CEO");
Person person("张三",40,"男");
r.setValue(person);
cout << r.getKey() <<" : ";
r.getValue().display();
return 0;
}