标题:大侠帮忙看看,哪错啦?
只看楼主
lucky_boy
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2008-4-3
 问题点数:0 回复次数:12 
大侠帮忙看看,哪错啦?
编译,连接都没问题,运行到最后才报错,咋回事?
而且,要是把析构函数删了的话,就没问题!
问题该出在析构函数或是内存分配吧
大侠帮忙看看哈
#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();
}
搜索更多相关主题的帖子: include person public 而且 
2008-04-09 07:34
lucky_boy
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2008-4-3
得分:0 
【超难题】
没人能解决吗?
自己顶一下,
免得沉了
2008-04-09 14:28
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
得分:0 
我看了一下..用delete只能删除new创建的内存..你要保证你在删除之前一定要创建

[[it] 本帖最后由 sunkaidong 于 2008-4-9 14:56 编辑 [/it]]

学习需要安静。。海盗要重新来过。。
2008-04-09 14:50
lucky_boy
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2008-4-3
得分:0 
void setName(char *name)
    {
        cName = new char[sizeof(name)];
        strcpy(cName, name);
    }

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

已经创建了呀!
2008-04-09 16:17
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
得分:0 
cName = new char[sizeof(name)+1];
 cID = new char[sizeof(id)+1];
仔细看看

学习需要安静。。海盗要重新来过。。
2008-04-09 16:32
lucky_boy
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2008-4-3
得分:0 
不是吧
还是不行呀
cName = new char[strlen(name)+1];
cID = new char[strlen(id)+1];

cName = new char[sizeof(name)];
cID = new char[sizeof(id)];
是等效的吧,
这儿应该没错啊
2008-04-09 18:12
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
得分:0 
我的可以运行..给\0留下位置..还有不要用sizeof..这里只是凑巧可以用..自己体会

学习需要安静。。海盗要重新来过。。
2008-04-09 18:23
lucky_boy
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2008-4-3
得分:0 
万非感谢,就是错在这儿了
sizeof(name)算的是char*的大小,一直都是4
对吧 :)
2008-04-09 22:45
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
得分:0 
一旦程序运行异常的时候一般都和指针有关系..你可以看看你指针是不是空的.数组空间是不是够...

学习需要安静。。海盗要重新来过。。
2008-04-09 22:51
xjywc
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2008-1-11
得分:0 
学习了,楼上真是强啊
2008-04-10 20:40



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-207901-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.087700 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved