C++类和对象,多态性,继承,虚函数虚基类

解决代码存在的问题:terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc,代码输入到graduate的数据后就终止了,如何解决?求解正确输入输出!!!
people类派生出student类,添加属性:班号string classNO;从people类派生出teacher类,添加属性:职务string principalship、部门string department。从student类中派生出graduate类,添加属性:专业stringr subject、导师teacher advisor;从graduate类和teacher类中派生出TA(助教)类,注意虚基类的使用。用成员函数实现对人员信息的录入和显示。附:people类的属性:number(编号)、sex(性别)、birthday(出生日期)、id(身份证号)等等。其中“出生日期”声明为一个“日期”类内嵌子对象。

#include <iostream>
#include <string>
using namespace std;

// 定义出生日期Bir内嵌类
class Bir
{
public:
    Bir() {}
    Bir(int Y, int M, int D) : year(Y), month(M), day(D)
    {
        cout << "Bir构造函数执行中......" << endl;
    };
    ~Bir()
    {
        cout << "Bir析构函数执行中......" << endl;
    }
    Bir(const Bir &other) : year(other.year), month(other.month), day(other.day) { cout << "Bir复制构造函数执行中......" << endl; };
    inline int Get_year() const { return year; }
    inline int Get_month() const { return month; }
    inline int Get_day() const { return day; }

private:
    int year;
    int month;
    int day;
};

// 定义人员管理类People
class People
{
public:
    People() {}
    People(string Number, string Sex, Bir Birthday, string Id)
        : number(Number), sex(Sex), birthday(Birthday), id(Id) { cout << "People构造中......" << endl; };
    ~People() { cout << "People析构中......" << endl; }
    virtual void print() const;
    People(const People &other)
        : number(other.number), sex(other.sex), birthday(other.birthday), id(other.id) { cout << "People复制构造中......" << endl; };

protected:
    string number;
    string sex;
    Bir birthday;
    string id;
};
void People::print() const
{
    cout << "number: " << number << endl;
    cout << "sex: " << sex << endl;
    cout << "birthday: " << birthday.Get_year() << "年" << birthday.Get_month() << "月" << birthday.Get_day() << "日" << endl;
    cout << "id: " << id << endl;
}
class Student : virtual public People
{
protected:
    string classNO;

public:
    Student() {}
    Student(string Number, string Sex, Bir Birthday, string Id, string classno) : People(Number, Sex, Birthday, Id), classNO(classno)
    {
        cout << "Student构造中......" << endl;
    }
    ~Student()
    {
        cout << "Student析构中......" << endl;
    }
    Student(const Student &other)
        : People(number, sex, birthday, id), classNO(other.classNO) { cout << "Student复制构造中......" << endl; };
    virtual void print() const
    {
        cout << "number: " << number << endl;
        cout << "sex: " << sex << endl;
        cout << "birthday: " << birthday.Get_year() << "年" << birthday.Get_month() << "月" << birthday.Get_day() << "日" << endl;
        cout << "id: " << id << endl;
        cout << "classno: " << classNO << endl;
    }
};
class Teacher : virtual public People
{
protected:
    string Principalship;
    string Department;

public:
    Teacher() {}
    Teacher(string Number, string Sex, Bir Birthday, string Id, string principalship, string department) : People(Number, Sex, Birthday, Id), Principalship(principalship), Department(department)
    {
        cout << "Teacher构造中......" << endl;
    }
    Teacher(const Teacher &other) : People(number, sex, birthday, id), Principalship(other.Principalship), Department(other.Department)
    {
        cout << "Teacher复制构造中......" << endl;
    }
    virtual void print() const
    {
        cout << "number: " << number << endl;
        cout << "sex: " << sex << endl;
        cout << "birthday: " << birthday.Get_year() << "年" << birthday.Get_month() << "月" << birthday.Get_day() << "日" << endl;
        cout << "id: " << id << endl;
        cout << "Principalship: " << Principalship << endl;
        cout << "Department: " << Department << endl;
    }
    ~Teacher()
    {
        cout << "Teacher析构中......" << endl;
    }
};
class Graduate : virtual public Student
{
protected:
    string Subject;
    Teacher Advisor;

public:
    Graduate() {}
    Graduate(string Number, string Sex, Bir Birthday, string Id, string classno, string subject, Teacher advisor) : Student(Number, Sex, Birthday, Id, classno), Subject(subject), Advisor(advisor)
    {
        cout << "Graduate构造中......" << endl;
    }
    Graduate(const Graduate &other) : Student(number, sex, birthday, id, classNO), Subject(other.Subject), Advisor(other.Advisor)
    {
        cout << "Graduate复制构造中......" << endl;
    }
    virtual void print() const
    {
        Advisor.print();
        cout << "Subject: " << Subject << endl;
    }
    ~Graduate()
    {
        cout << "Graduate析构中......" << endl;
    }
};
class TA : virtual public Graduate, virtual public Teacher
{
public:
    TA() {}
    TA(string Number, string Sex, Bir Birthday, string Id, string classno, string subject, Teacher advisor, string principalship, string department) : Graduate(
                                                                                                                                                           Number, Sex, Birthday, Id, classno, subject, advisor),
                                                                                                                                                       Teacher(Number, Sex, Birthday, Id, principalship, department)
    {
        cout << "TA构造中......" << endl;
    }
    TA(const TA &other) : Graduate(number, sex, birthday, id, classNO, Subject, Advisor),
                          Teacher(number, sex, birthday, id, Principalship, Department)
    {
        cout << "TA复制构造中......" << endl;
    }
    virtual void print() const
    {
        cout << "number: " << number << endl;
        cout << "sex: " << sex << endl;
        cout << "birthday: " << birthday.Get_year() << "年" << birthday.Get_month() << "月" << birthday.Get_day() << "日" << endl;
        cout << "id: " << id << endl;
        cout << "Principalship: " << Principalship << endl;
        cout << "Department: " << Department << endl;
        cout << "Subject: " << Subject << endl;
    }
    ~TA()
    {
        cout << "TA析构中......" << endl;
    }
};
void test()
{

    string Number, Sex, Id, classno, subject, principalship, department;
    int YEAR, MONTH, DAY, n1, n2, n3, n4;
    Teacher tea1;
    Teacher tea2;
    cout << "Student:" << endl;
    cin >> n1;
    Student *stu = new Student[n1];
    for (int i = 0; i < n1; i++)
    {
        cout << "Input number: " << endl;
        cin >> Number;
        cout << "Input sex: " << endl;
        cin >> Sex;
        cout << "Input birthday:" << endl;
        cin >> YEAR >> MONTH >> DAY;
        Bir bir_thday(YEAR, MONTH, DAY);
        cout << "Input id:" << endl;
        cin >> Id;
        cout << "Input classno:" << endl;
        cin >> classno;
        stu[i] = Student(Number, Sex, bir_thday, Id, classno);
    }
    cout << "Teacher:" << endl;
    cin >> n2;
    Teacher *tea = new Teacher[n2];
    for (int i = 0; i < n1; i++)
    {
        cout << "Input number: " << endl;
        cin >> Number;
        cout << "Input sex: " << endl;
        cin >> Sex;
        cout << "Input birthday:" << endl;
        cin >> YEAR >> MONTH >> DAY;
        Bir bir_thday(YEAR, MONTH, DAY);
        cout << "Input id:" << endl;
        cin >> Id;
        cout << "Input principalship:" << endl;
        cin >> principalship;
        cout << "Input department:" << endl;
        cin >> department;
        tea[i] = Teacher(Number, Sex, bir_thday, Id, principalship, department);
    }
    cout << "Graduate:" << endl;
    cin >> n3;
    Graduate *gra = new Graduate[n3];
    for (int i = 0; i < n3; i++)
    {
        cout << "Input number: " << endl;
        cin >> Number;
        cout << "Input sex: " << endl;
        cin >> Sex;
        cout << "Input birthday:" << endl;
        cin >> YEAR >> MONTH >> DAY;
        Bir bir_thday(YEAR, MONTH, DAY);
        cout << "Input id:" << endl;
        cin >> Id;
        cout << "Input classno:" << endl;
        cin >> classno;
        cout << "Input subject:" << endl;
        cin >> subject;
        cout << "Input principalship:" << endl;
        cin >> principalship;
        cout << "Input department:" << endl;
        cin >> department;
        tea1 = Teacher(Number, Sex, bir_thday, Id, principalship, department);
        gra[i] = Graduate(Number, Sex, bir_thday, Id, classno, subject, tea1);
    }
    cout << "TA:" << endl;
    cin >> n4;
    TA *ta = new TA[n4];
    for (int i = 0; i < n4; i++)
    {
        cout << "Input number: " << endl;
        cin >> Number;
        cout << "Input sex: " << endl;
        cin >> Sex;
        cout << "Input birthday:" << endl;
        cin >> YEAR >> MONTH >> DAY;
        Bir bir_thday(YEAR, MONTH, DAY);
        cout << "Input id:" << endl;
        cin >> Id;
        cout << "Input classno:" << endl;
        cin >> classno;
        cout << "Input subject:" << endl;
        cin >> subject;
        cout << "Input principalship:" << endl;
        cin >> principalship;
        cout << "Input department:" << endl;
        cin >> department;
        tea2 = Teacher(Number, Sex, bir_thday, Id, principalship, department);
        ta[i] = TA(Number, Sex, bir_thday, Id, classno, subject, tea2, principalship, department);
    }
    for (int i = 0; i < n1; i++)
    {
        stu[i].print();
    }
    for (int i = 0; i < n2; i++)
    {
        tea[i].print();
    }
    for (int i = 0; i < n3; i++)
    {
        gra[i].print();
    }
    for (int i = 0; i < n4; i++)
    {
        ta[i].print();
    }
    delete[] stu;
    delete[] tea;
    delete[] gra;
    delete[] ta;
}
int main()
{
    test();
    return 0;
}



Teacher(const Teacher& other) : /*People(number, sex, birthday, id),*/ //把这里注释掉

该问题是内存分配失败导致的,可能是因为程序申请的内存超过了计算机所能提供的内存空间,或者是内存出现了错误。

解决方法:

1.在程序中加入try-catch语句,捕获std::bad_alloc异常,并在异常处理程序中释放不需要的内存,或者重新设计程序,减少内存占用。

2.使用现代的动态内存分配方式,如智能指针和容器类,来管理内存分配和释放,避免手动分配和释放内存。

3.检查代码中是否存在内存泄漏、重复释放等问题,如果有,及时修复。

以下是参考代码(仅供参考):

#include 
#include 
#include 
#include 
#include 
using namespace std;

//日期类
class Date {
public:
    Date(int y, int m, int d) : year(y), month(m), day(d) {}
    int getYear() const { return year; }
    int getMonth() const { return month; }
    int getDay() const { return day; }
private:
    int year;
    int month;
    int day;
};

//人员信息类
class People {
public:
    People(int n, char s, int y, int m, int d, const string& id) : number(n), sex(s), birthday(y, m, d), idCard(id) {}
    virtual ~People() {}
    virtual void showInfo() const {
        cout << "编号:" << number << endl;
        cout << "性别:" << sex << endl;
        cout << "出生日期:" << birthday.getYear() << "-" << birthday.getMonth() << "-" << birthday.getDay() << endl;
        cout << "身份证号:" << idCard << endl;
    }
protected:
    int number;
    char sex;
    Date birthday;
    string idCard;
};

//学生类
class Student : public People {
public:
    Student(int n, char s, int y, int m, int d, const string& id, const string& cn) : People(n, s, y, m, d, id), classNO(cn) {}
    virtual ~Student() {}
    virtual void showInfo() const {
        People::showInfo();
        cout << "班号:" << classNO << endl;
    }
protected:
    string classNO;
};

//教师类
class Teacher : virtual public People {
public:
    Teacher(int n, char s, int y, int m, int d, const string& id, const string& p, const string& dep) : People(n, s, y, m, d, id), principalship(p), department(dep) {}
    virtual ~Teacher() {}
    virtual void showInfo() const {
        People::showInfo();
        cout << "职务:" << principalship << endl;
        cout << "部门:" << department << endl;
    }
protected:
    string principalship;
    string department;
};

//研究生类
class Graduate : public Student, virtual public Teacher {
public:
    Graduate(int n, char s, int y, int m, int d, const string& id, const string& cn, const string& sub, Teacher* adv) : People(n, s, y, m, d, id), Student(n, s, y, m, d, id, cn), Teacher(n, s, y, m, d, id, "导师", "研究生院"), subject(sub), advisor(adv) {}
    virtual ~Graduate() {}
    virtual void showInfo() const {
        People::showInfo();
        cout << "班号:" << classNO << endl;
        cout << "专业:" << subject << endl;
        cout << "导师信息:" << endl;
        advisor->showInfo();
    }
private:
    string subject;
    Teacher* advisor;
};

//助教类
class TA : public Graduate, public Teacher {
public:
    TA(int n, char s, int y, int m, int d, const string& id, const string& cn, const string& sub, Teacher* adv) : People(n, s, y, m, d, id), Graduate(n, s, y, m, d, id, cn, sub, adv), Teacher(n, s, y, m, d, id, "助教", "学院") {}
    virtual ~TA() {}
    virtual void showInfo() const {
        People::showInfo();
        cout << "班号:" << classNO << endl;
        cout << "专业:" << subject << endl;
        cout << "导师信息:" << endl;
        advisor->showInfo();
        cout << "职务:" << principalship << endl;
        cout << "部门:" << department << endl;
    }
};

int main() {
    vector<shared_ptr> peopleList;
    Teacher* t1 = new Teacher(1001, 'M', 1970, 9, 1, "42011119700901999X", "教授", "计算机学院");
    Graduate* g1 = new Graduate(101, 'F', 1996, 9, 1, "420111199609019999", "计算机科学与技术", "数据库", t1);
    TA* ta1 = new TA(201, 'M', 1998, 9, 1, "420111199809019999", "计算机科学与技术", "数据库", t1);
    peopleList.push_back(shared_ptr(t1));
    peopleList.push_back(shared_ptr(g1));
    peopleList.push_back(shared_ptr(ta1));
    for (const auto& p : peopleList) {
        p->showInfo();
        cout << endl;
    }
    return 0;
}

Teacher(const Teacher &other) : People(number, sex, birthday, id), Principalship(other.Principalship), Department(other.Department)
    {
        cout << "Teacher复制构造中......" << endl;
    }

这段代码有问题,要改成


    Teacher(const Teacher &other) : People(other.number, other.sex, other.birthday, other.id), Principalship(other.Principalship), Department(other.Department)
    {
        cout << "Teacher复制构造中......" << endl;
    }

而要避免这个错误也很简单,在编码习惯上可以避免,只要在写成员变量时添加m_前缀就可以了,编译器就会给你报错了

构造函数有问题,参考如下代码:


#include <iostream>
#include <string>
using namespace std;
// 定义出生日期Bir内嵌类
class Bir
{
public:
    Bir() {}
    Bir(int Y, int M, int D) : year(Y), month(M), day(D)
    {
        cout << "Bir构造函数执行中......" << endl;
    };
    ~Bir()
    {
        cout << "Bir析构函数执行中......" << endl;
    }
    Bir(const Bir &other) : year(other.year), month(other.month), day(other.day) { cout << "Bir复制构造函数执行中......" << endl; };
    inline int Get_year() const { return year; }
    inline int Get_month() const { return month; }
    inline int Get_day() const { return day; }

private:
    int year;
    int month;
    int day;
};

// 定义人员管理类People
class People
{
public:

    People(string Number, string Sex, Bir Birthday, string Id)
        : number(Number), sex(Sex), birthday(Birthday), id(Id) {
        cout << "People构造中......" << endl;
    };
    ~People() { cout << "People析构中......" << endl; }
    virtual void print() const;
    People(const People &other)
        : number(other.number), sex(other.sex), birthday(other.birthday), id(other.id) {
        cout << "People复制构造中......" << endl;
    };

protected:
    string number;
    string sex;
    Bir birthday;
    string id;
};
void People::print() const
{
    cout << "number: " << number << endl;
    cout << "sex: " << sex << endl;
    cout << "birthday: " << birthday.Get_year() << "年" << birthday.Get_month() << "月" << birthday.Get_day() << "日" << endl;
    cout << "id: " << id << endl;
}
class Student : virtual public People
{
protected:
    string classNO;

public:
    Student() : People("0", "0", Bir(0,0,0), "0"), classNO("0")
    {}

    Student(string Number, string Sex, Bir Birthday, string Id, string classno) : People(Number, Sex, Birthday, Id), classNO(classno)
    {
        cout << "Student构造中......" << endl;
    }
    ~Student()
    {
        cout << "Student析构中......" << endl;
    }
    Student(const Student &other)
        : People(other.number, other.sex, other.birthday, other.id), classNO(other.classNO) {
        cout << "Student复制构造中......" << endl;
    };
    virtual void print() const
    {
        cout << "number: " << number << endl;
        cout << "sex: " << sex << endl;
        cout << "birthday: " << birthday.Get_year() << "年" << birthday.Get_month() << "月" << birthday.Get_day() << "日" << endl;
        cout << "id: " << id << endl;
        cout << "classno: " << classNO << endl;
    }
};
class Teacher : virtual public People
{
protected:
    string Principalship;
    string Department;

public:
    Teacher() : People("0", "0", Bir(0, 0, 0), "0"), Principalship("0"), Department("0") {
    }

    Teacher(string Number, string Sex, Bir Birthday, string Id, string principalship, string department) : People(Number, Sex, Birthday, Id), Principalship(principalship), Department(department)
    {
        cout << "Teacher构造中......" << endl;
    }
    Teacher(const Teacher &other) : People(other.number, other.sex, other.birthday, other.id), Principalship(other.Principalship), Department(other.Department)
    {
        cout << "Teacher复制构造中......" << endl;
    }
    virtual void print() const
    {
        cout << "number: " << number << endl;
        cout << "sex: " << sex << endl;
        cout << "birthday: " << birthday.Get_year() << "年" << birthday.Get_month() << "月" << birthday.Get_day() << "日" << endl;
        cout << "id: " << id << endl;
        cout << "Principalship: " << Principalship << endl;
        cout << "Department: " << Department << endl;
    }
    ~Teacher()
    {
        cout << "Teacher析构中......" << endl;
    }
};
class Graduate : virtual public Student
{
protected:
    string Subject;
    Teacher Advisor;

public:
    Graduate() : Student("0", "0", Bir(0, 0, 0), "0", "0"), Subject("0"), Advisor(Teacher()), People("0", "0", Bir(0, 0, 0), "0")
    {

    }

    Graduate(string Number, string Sex, Bir Birthday, string Id, string classno, string subject, Teacher advisor) : Student(Number, Sex, Birthday, Id, classno), Subject(subject), Advisor(advisor), People(Number, Sex, Birthday, Id)
    {
        cout << "Graduate构造中......" << endl;
    }
    Graduate(const Graduate &other) : Student(other.number, other.sex, other.birthday, other.id, other.classNO), Subject(other.Subject), Advisor(other.Advisor), People(other.number, other.sex, other.birthday, other.id)
    {
        cout << "Graduate复制构造中......" << endl;
    }
    virtual void print() const
    {
        Advisor.print();
        cout << "Subject: " << Subject << endl;
    }
    ~Graduate()
    {
        cout << "Graduate析构中......" << endl;
    }
};
class TA : virtual public Graduate, virtual public Teacher
{
public:
    TA() : Graduate(),
        Teacher(), People("0", "0", Bir(0, 0, 0), "0"), Student()
    {
        cout << "TA构造中......" << endl;
    }

    TA(string Number, string Sex, Bir Birthday, string Id, string classno, string subject, Teacher advisor, string principalship, string department) : Graduate(
        Number, Sex, Birthday, Id, classno, subject, advisor),
        Teacher(Number, Sex, Birthday, Id, principalship, department),People(Number, Sex, Birthday, Id), Student(Number, Sex, Birthday, Id, classno)
    {
        cout << "TA构造中......" << endl;
    }
    TA(const TA &other) : Graduate(other.number, other.sex, other.birthday, other.id, other.classNO, other.Subject, other.Advisor),
        Teacher(other.number, other.sex, other.birthday, other.id, other.Principalship, other.Department), People(other.number, other.sex, other.birthday, other.id), Student(other.number, other.sex, other.birthday, other.id, other.classNO)
    {
        cout << "TA复制构造中......" << endl;
    }
    virtual void print() const
    {
        cout << "number: " << number << endl;
        cout << "sex: " << sex << endl;
        cout << "birthday: " << birthday.Get_year() << "年" << birthday.Get_month() << "月" << birthday.Get_day() << "日" << endl;
        cout << "id: " << id << endl;
        cout << "Principalship: " << Principalship << endl;
        cout << "Department: " << Department << endl;
        cout << "Subject: " << Subject << endl;
    }
    ~TA()
    {
        cout << "TA析构中......" << endl;
    }
};
void test()
{

    string Number, Sex, Id, classno, subject, principalship, department;
    int YEAR, MONTH, DAY, n1, n2, n3, n4;
    cout << "Student:" << endl;
    cin >> n1;
    Student *stu = new Student[n1];
    for (int i = 0; i < n1; i++)
    {
        cout << "Input number: " << endl;
        cin >> Number;
        cout << "Input sex: " << endl;
        cin >> Sex;
        cout << "Input birthday:" << endl;
        cin >> YEAR >> MONTH >> DAY;
        Bir bir_thday(YEAR, MONTH, DAY);
        cout << "Input id:" << endl;
        cin >> Id;
        cout << "Input classno:" << endl;
        cin >> classno;
        stu[i] = Student(Number, Sex, bir_thday, Id, classno);
    }
    cout << "Teacher:" << endl;
    cin >> n2;
    Teacher *tea = new Teacher[n2];
    for (int i = 0; i < n2; i++)
    {
        cout << "Input number: " << endl;
        cin >> Number;
        cout << "Input sex: " << endl;
        cin >> Sex;
        cout << "Input birthday:" << endl;
        cin >> YEAR >> MONTH >> DAY;
        Bir bir_thday(YEAR, MONTH, DAY);
        cout << "Input id:" << endl;
        cin >> Id;
        cout << "Input principalship:" << endl;
        cin >> principalship;
        cout << "Input department:" << endl;
        cin >> department;
        tea[i] = Teacher(Number, Sex, bir_thday, Id, principalship, department);
    }
    cout << "Graduate:" << endl;
    cin >> n3;
    Graduate *gra = new Graduate[n3];
    for (int i = 0; i < n3; i++)
    {
        cout << "Input number: " << endl;
        cin >> Number;
        cout << "Input sex: " << endl;
        cin >> Sex;
        cout << "Input birthday:" << endl;
        cin >> YEAR >> MONTH >> DAY;
        Bir bir_thday(YEAR, MONTH, DAY);
        cout << "Input id:" << endl;
        cin >> Id;
        cout << "Input classno:" << endl;
        cin >> classno;
        cout << "Input subject:" << endl;
        cin >> subject;
        cout << "Input principalship:" << endl;
        cin >> principalship;
        cout << "Input department:" << endl;
        cin >> department;
        Teacher tea1(Number, Sex, bir_thday, Id, principalship, department);
        gra[i] = Graduate(Number, Sex, bir_thday, Id, classno, subject, tea1);
    }
    cout << "TA:" << endl;
    cin >> n4;
    TA *ta = new TA[n4];
    for (int i = 0; i < n4; i++)
    {
        cout << "Input number: " << endl;
        cin >> Number;
        cout << "Input sex: " << endl;
        cin >> Sex;
        cout << "Input birthday:" << endl;
        cin >> YEAR >> MONTH >> DAY;
        Bir bir_thday(YEAR, MONTH, DAY);
        cout << "Input id:" << endl;
        cin >> Id;
        cout << "Input classno:" << endl;
        cin >> classno;
        cout << "Input subject:" << endl;
        cin >> subject;
        cout << "Input principalship:" << endl;
        cin >> principalship;
        cout << "Input department:" << endl;
        cin >> department;
        Teacher tea2(Number, Sex, bir_thday, Id, principalship, department);
        ta[i] = TA(Number, Sex, bir_thday, Id, classno, subject, tea2, principalship, department);
    }
    for (int i = 0; i < n1; i++)
    {
        stu[i].print();
    }
    for (int i = 0; i < n2; i++)
    {
        tea[i].print();
    }
    for (int i = 0; i < n3; i++)
    {
        gra[i].print();
    }
    for (int i = 0; i < n4; i++)
    {
        ta[i].print();
    }
    delete[] stu;
    delete[] tea;
    delete[] gra;
    delete[] ta;
}
int main()
{
    test();
    return 0;
}

可能是这两个问题
【内存不够】:

1,确认系统已占用内存是否正常,排除数据量过大导致的问题,此时系统内存不足导致 std::bad_alloc

【内存剩余】:

1,确认接口调用时,调用和背调接口的的参数是否一致,动态库库调用中若不一致,编译链接通过,但执行可能导致 std::bad_alloc

2,确认是否使用vector,vector超容量时会重新申请二倍内存,因为vector会将老的一块内存,完全拷贝到另一块连续容量为2倍的vector内存中,高峰时内存将时当前系统的3倍,此时可能导致 std::bad_alloc

"terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc" 错误在内存分配失败时会出现。当程序使用 new 运算符无法分配内存时,就会抛出此错误。

在你的代码中,这个错误可能是由于使用 new 分配大型数组引起的,例如 Student *stu = new Student[n1];、Teacher *tea = new Teacher[n2]; 等等

此类报错的原因多为内存不够导致程序异常终止,所以可以检查一下自己声明的某一部分是否导致了内存不够.

该回答引用GPT
代码中存在一处错误,即第36行应该添加 virtual 关键字,将 People 类声明为虚基类。这样可以避免在派生类 TA 中出现两份 People 的子对象,从而避免因两份 People 子对象析构时重复释放出错的问题。修改后的代码如下:

class Student : virtual public People
{
    // ...
};

class Teacher : virtual public People
{
    // ...
};

class Graduate : virtual public Student
{
    // ...
};

class TA : virtual public Graduate, virtual public Teacher
{
    // ...
};

另外,在输入数据时,第二个循环输入 Teacher 对象时,循环次数应该为 n2 而非 n1,已经修正。

基于new bing的修改:
码中存在的问题是在输入数据的时候没有对动态开辟的数组进行越界检查,可能会导致内存访问错误和std::bad_alloc异常。可以在输入数据之前先判断输入的数据量是否大于0并且小于等于数组的长度,如果不符合要求则提示重新输入

#include<iostream>
#include<string>
using namespace std;

class Bir {
public:
    Bir(int year, int month, int day)
        :year_(year), month_(month), day_(day) {}
    void print_bir() {
        cout << year_ << "-" << month_ << "-" << day_ << endl;
    }
private:
    int year_;
    int month_;
    int day_;
};

class People {
public:
    People(string number, string sex, Bir bir_thday, string id)
        :number_(number), sex_(sex), bir_thday_(bir_thday), ID_(id) {}
    void print() {
        cout << "Number:" << number_ << endl;
        cout << "Sex:" << sex_ << endl;
        cout << "ID:" << ID_ << endl;
        cout << "Birthday:";
        bir_thday_.print_bir();
    }
private:
    string number_;
    string sex_;
    Bir bir_thday_;
    string ID_;
};

class Student : public People {
public:
    Student(string number, string sex, Bir bir_thday, string id, string classno)
        :People(number, sex, bir_thday, id), classno_(classno) {}
    void print() {
        People::print();
        cout << "ClassNo:" << classno_ << endl;
    }
private:
    string classno_;
};

class Teacher : public People {
public:
    Teacher(string number, string sex, Bir bir_thday, string id, string principalship, string department)
        :People(number, sex, bir_thday, id), principalship_(principalship), department_(department) {}
    void print() {
        People::print();
        cout << "Principalship:" << principalship_ << endl;
        cout << "Department:" << department_ << endl;
    }
private:
    string principalship_;
    string department_;
};

class Graduate : public Student {
public:
    Graduate(string number, string sex, Bir bir_thday, string id, string classno, string subject, Teacher advisor)
        :Student(number, sex, bir_thday, id, classno), subject_(subject), advisor_(advisor) {}
    void print() {
        Student::print();
        cout << "Subject:" << subject_ << endl;
        cout << "Advisor:";
        advisor_.print();
    }
private:
    string subject_;
    Teacher advisor_;
};

class TA : public Graduate {
public:
    TA(string number, string sex, Bir bir_thday, string id, string classno, string subject, Teacher advisor, string principalship, string department)
        :Graduate(number, sex, bir_thday, id, classno, subject, advisor), principalship_(principalship), department_(department) {}
    void print() {
        Graduate::print();
        cout << "DepartMent:" << department_ << endl;
        cout << "Principalship:" << principalship_ << endl;
    }
private:
    string principalship_;
    string department_;
};

void test()
{
    string Number, Sex, Id, classno, subject, principalship, department;
    int YEAR, MONTH, DAY, n1, n2, n3, n4;
    Teacher tea1;
    Teacher tea2;
    cout << "Student:" << endl;
    cin >> n1;
    if (n1 <= 0) {
        cerr << "Invalid input!" << endl;
        return;
    }
    Student *stu = new Student[n1];
    for (int i = 0; i < n1; i++)
    {
        cout << "Input number: " << endl;
        cin >> Number;
        cout << "Input sex: " << endl;
        cin >> Sex;
        cout << "Input birthday:" << endl;
        cin >> YEAR >> MONTH >> DAY;
        Bir bir_thday(YEAR, MONTH, DAY);
        cout << "Input id:" << endl;
        cin >> Id;
        cout << "Input classno:" << endl;
        cin >> classno;
        stu[i] = Student(Number, Sex, bir_thday, Id, classno);
    }
    cout << "Teacher:" << endl;
    cin >> n2;
    if (n2 <= 0) {
        cerr << "Invalid input!" << endl;
        return;
    }
    Teacher *tea = new Teacher[n2];
    for (int i = 0; i < n2; i++)
    {
        cout << "Input number: " << endl;
        cin >> Number;
        cout << "Input sex: " << endl;
        cin >> Sex;
        cout << "Input birthday:" << endl;
        cin >> YEAR >> MONTH >> DAY;
        Bir bir_thday(YEAR, MONTH, DAY);
        cout << "Input id:" << endl;
        cin >> Id;
        cout << "Input principalship:" << endl;
        cin >> principalship;
        cout << "Input department:" << endl;
        cin >> department;
        tea[i] = Teacher(Number, Sex, bir_thday, Id, principalship, department);
    }
    cout << "Graduate:" << endl;
    cin >> n3;
    if (n3 <= 0) {
        cerr << "Invalid input!" << endl;
        return;
    }
    Graduate *gra = new Graduate[n3];
    for (int i = 0; i < n3; i++)
    {
        cout << "Input number: " << endl;
        cin >> Number;
        cout << "Input sex: " << endl;
        cin >> Sex;
        cout << "Input birthday:" << endl;
        cin >> YEAR >> MONTH >> DAY;
        Bir bir_thday(YEAR, MONTH, DAY);
        cout << "Input id:" << endl;
        cin >> Id;
        cout << "Input classno:" << endl;
        cin >> classno;
        cout << "Input subject:" << endl;
        cin >> subject;
        cout << "Input principalship:" << endl;
        cin >> principalship;
        cout << "Input department:" << endl;
        cin >> department;
        tea1 = Teacher(Number, Sex, bir_thday, Id, principalship, department);
        gra[i] = Graduate(Number, Sex, bir_thday, Id, classno, subject, tea1);
    }
    cout << "TA:" << endl;
    cin >> n4;
    if (n4 <= 0) {
        cerr << "Invalid input!" << endl;
        return;
    }
    TA *ta = new TA[n4];
    for (int i = 0; i < n4; i++)
    {
        cout << "Input number: " << endl;
        cin >> Number;
        cout << "Input sex: " << endl;
        cin >> Sex;
        cout << "Input birthday:" << endl;
        cin >> YEAR >> MONTH >> DAY;
        Bir bir_thday(YEAR, MONTH, DAY);
        cout << "Input id:" << endl;
        cin >> Id;
        cout << "Input classno:" << endl;
        cin >> classno;
        cout << "Input subject:" << endl;
        cin >> subject;
        cout << "Input principalship:" << endl;
        cin >> principalship;
        cout << "Input department:" << endl;
        cin >> department;
        tea2 = Teacher(Number, Sex, bir_thday, Id, principalship, department);
        ta[i] = TA(Number, Sex, bir_thday, Id, classno, subject, tea2, principalship, department);
    }
    for (int i = 0; i < n1; i++)
    {
        stu[i].print();
    }
    for (int i = 0; i < n2; i++)
    {
        tea[i].print();
    }
    for (int i = 0; i < n3; i++)
    {
        gra[i].print();
    }
    for (int i = 0; i < n4; i++)
    {
        ta[i].print();
    }
    delete[] stu;
    delete[] tea;
    delete[] gra;
    delete[] ta;
}

int main()
{
    test();
    return 0;
}


这个错误是因为程序运行时内存不足而导致 std::bad_alloc 异常。可能是在输入数据时,分配的内存超出了系统可用内存,或者是在程序的其他地方也有类似的内存分配问题。解决办法可以尝试以下几点:

  1. 检查数据输入是否合法,如是否在循环中不断分配内存导致内存占用过大。

  2. 尝试优化代码,减少内存的占用,可以通过指针、引用等方式来避免不必要的内存拷贝和分配。

  3. 调节系统资源分配策略,如增加 swap 空间等。
    嗯 想到的暂时只有这些 望采纳

这个错误通常是由于内存不足导致的。当程序尝试分配的内存超过了操作系统或者程序可用的内存时,就会抛出std::bad_alloc异常。这通常发生在以下情况下:

  1. 程序中使用了太多的动态内存分配,导致内存不足。
  2. 程序中存在内存泄漏,导致内存不断增加,最终耗尽了可用内存。
  3. 程序中存在死循环或递归调用,导致栈空间不断增加,最终耗尽了可用内存。
    针对这个错误,可以尝试以下几种方法:
  4. 优化程序中的内存使用,减少不必要的动态内存分配,避免内存泄漏。
  5. 检查程序中是否存在死循环或递归调用,避免栈空间不足。
  6. 增加可用内存,可以通过升级硬件、增加虚拟内存等方式来解决。
  7. 如果是在处理大数据量时出现的问题,可以考虑使用流式处理或分批处理的方式来降低内存占用。