标题:大侠帮忙看看,哪错啦?
取消只看楼主
lucky_boy
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2008-4-3
 问题点数:0 回复次数:5 
大侠帮忙看看,哪错啦?
编译,连接都没问题,运行到最后才报错,咋回事?
而且,要是把析构函数删了的话,就没问题!
问题该出在析构函数或是内存分配吧
大侠帮忙看看哈
#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
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
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
lucky_boy
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2008-4-3
得分:0 
万非感谢,就是错在这儿了
sizeof(name)算的是char*的大小,一直都是4
对吧 :)
2008-04-09 22:45
lucky_boy
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2008-4-3
得分:0 
回复 9# 的帖子
感谢你没有直接告诉我答案
否则我可能现在还不明白strlen和sizeof的区别
还要感谢你的编程经验
2008-04-11 17:03



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




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

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