注册 登录
编程论坛 C++教室

大侠帮忙看看,哪错啦?

lucky_boy 发布于 2008-04-09 07:34, 932 次点击
编译,连接都没问题,运行到最后才报错,咋回事?
而且,要是把析构函数删了的话,就没问题!
问题该出在析构函数或是内存分配吧
大侠帮忙看看哈
#include<iostream.h>
#include<string.h>
class CPerson
{
public:
    CPerson(char *name = NULL, char *id = NULL, int sex = 0)
    {
        Input(name, id, sex);
        cout<<"%%%%%CPerson Constructor%%%"<<endl;
    }
    CPerson(const CPerson &person)
    {
        Input(, person.cID, person.bSex);
        cout<<"Copy constructor has been used."<<endl;
    }
virtual    ~CPerson()
    {
    delete []cName;
    delete []cID;
    cout<<"########CPerson Destroyed!!!"<<endl;
    }
    void Input(char *name, char *id, int sex)
    {
        setName(name);
        setID(id);
        setSex(sex);
    }
    void Output()
    {
        cout<<"NAME: "<<cName<<endl;
        cout<<"ID: "<<cID<<endl;
        char *str = (bSex == 0 ? "man" : "woman");
        cout<<"SEX: "<<str<<endl;
    }
    char *getName()const
    {
        return cName;
    }
    void setName(char *name)
    {
        cName = new char[sizeof(name)];
        strcpy(cName, name);
    }
    char *getID()const
    {
        return cID;
    }
    void setID(char *id)
    {
        cID = new char[sizeof(id)];
        strcpy(cID, id);
    }
    int getSex()
    {
        return bSex;
    }
    void setSex(int sex)
    {
        bSex = sex;
    }
private:
    char *cName;
    char *cID;
    int bSex;
};

class CStudent: public CPerson
{
public:
    CStudent(char *name, char *id, int sex, double x = 0, double y = 0, double z = 0):CPerson(name, id, sex)
    {
        dbScore[0] = x;
        dbScore[1] = y;
        dbScore[2] = z;
        cout<<"CStudent Constructor"<<endl;
    }
    ~CStudent(){cout<<"$$$$$$$$$$$$CStudent Destroyed!!!"<<endl;}
    void Print()
    {
        cout<<"Student resume: "<<endl;
        Output();
        cout<<"GRADE: "<<dbScore[0]<<"\t"<<dbScore[1]<<"\t"<<dbScore[2]<<endl;
    }
private:
    double dbScore[3];
};

class CTeacher: public CPerson
{
public:
    CTeacher(char *name, char *id, int sex, int schoolage = 0):CPerson(name, id, sex)
    {
        OfSchoolAge = schoolage;
        cout<<"CTeacher Constructor"<<endl;
    }
    ~CTeacher(){cout<<"^^^^^^^^^^^^CTeacher DesTroyed!!!"<<endl;}
    void Print()
    {
        cout<<"Teacher Resume: "<<endl;
        Output();
        cout<<"SCHOOLAGE: "<<OfSchoolAge<<endl;
    }
private:
    int OfSchoolAge;
};

void BasicResume(char *name, char *id, int &sex)
{
    cout<<"Name: ";
    cin>>name;
    cout<<"ID: ";
    cin>>id;
    cout<<"Sex(man: 0, woman: 1): ";
    cin>>sex;
}

void StudentResume(char *name, char *id, int &sex, double *score)
{
    BasicResume(name, id, sex);
    cout<<"Grade(three subject): ";
    for(int i = 0; i < 3; i++)
    {
        cin>>score[i];
    }
    cout<<endl;
}

void TeacherResume(char *name, char *id, int &sex, int &schoolage)
{
    BasicResume(name, id, sex);
    cout<<"SchoolAge: ";
    cin>>schoolage;
}
void main()
{
    char name[20], id[20];
    int sex;
    double score[3];
    int schoolage;
    cout<<"Please input the student's resume: "<<endl;
    StudentResume(name, id, sex, score);
    CStudent student(name, id, sex, score[0], score[1], score[2]);
    student.Print();
    TeacherResume(name, id, sex, schoolage);
    CTeacher teacher(name, id, sex, schoolage);
    teacher.Print();
}
12 回复
#2
lucky_boy2008-04-09 14:28
【超难题】
没人能解决吗?
自己顶一下,
免得沉了
#3
sunkaidong2008-04-09 14:50
我看了一下..用delete只能删除new创建的内存..你要保证你在删除之前一定要创建

[[it] 本帖最后由 sunkaidong 于 2008-4-9 14:56 编辑 [/it]]
#4
lucky_boy2008-04-09 16:17
void setName(char *name)
    {
        cName = new char[sizeof(name)];
        strcpy(cName, name);
    }

void setID(char *id)
    {
        cID = new char[sizeof(id)];
        strcpy(cID, id);
    }

已经创建了呀!
#5
sunkaidong2008-04-09 16:32
cName = new char[sizeof(name)+1];
 cID = new char[sizeof(id)+1];
仔细看看
#6
lucky_boy2008-04-09 18:12
不是吧
还是不行呀
cName = new char[strlen(name)+1];
cID = new char[strlen(id)+1];

cName = new char[sizeof(name)];
cID = new char[sizeof(id)];
是等效的吧,
这儿应该没错啊
#7
sunkaidong2008-04-09 18:23
我的可以运行..给\0留下位置..还有不要用sizeof..这里只是凑巧可以用..自己体会
#8
lucky_boy2008-04-09 22:45
万非感谢,就是错在这儿了
sizeof(name)算的是char*的大小,一直都是4
对吧 :)
#9
sunkaidong2008-04-09 22:51
一旦程序运行异常的时候一般都和指针有关系..你可以看看你指针是不是空的.数组空间是不是够...
#10
xjywc2008-04-10 20:40
学习了,楼上真是强啊
#11
hyqzchh2008-04-11 07:07
没有问题
我用的运行了一下,也没有问题.
#12
lucky_boy2008-04-11 17:03
回复 9# 的帖子
感谢你没有直接告诉我答案
否则我可能现在还不明白strlen和sizeof的区别
还要感谢你的编程经验
#13
sunkaidong2008-04-11 17:06
不用客气..呵呵.一起提高...
1