繁体   English   中英

抛出异常

[英]Exception thrown

我正在做一个项目,我是初学者。 该项目涉及人、成人、儿童和转向架 class。 在转向架adultcount中,我将名为成人的类型指针初始化为adult = new Adult [adultcount] ,其中成人计数是用户想要添加的成人人数。
当我在输入中只添加一个成年人时,它工作正常,它接受输入并在 for 循环中分配值时分配它。
但是当我添加多个时,它会停止工作并在Adults[i].setdata()行抛出异常。

#include<iostream>
#include<string>
using namespace std;
class Person {
public:
    string Name;
    int Age;
    string Gender;
public:
    Person()
    {
        Name = '\0';
        Age = 0;
        Gender = '\0';
    }
    Person(string Name, int Age, string Gender)
    {
        this->Name = Name;
        this->Age = Age;
        this->Gender = Gender;
    }
    virtual void setdata()
    {
        string name, gender;
        int age;
        cin.ignore();
        cout << " enter Name :";
        getline(cin, name, '\n');
        this->Name = name;
        cout << endl;
        cout << " Enter gender ";
        getline(cin, gender, '\n');
        this->Gender = gender;
        cout << endl;
        cout << " Enter age ";
        cin >> age;
        Age = age;
        cout << endl;

    }
    virtual void printinfo()
    {
        cout << " name " <<Name;
        cout << " Gender " << Gender;
        cout << " age : " << Age;
    }

};
class Adult :public Person {
public:
    string Occupation;
    string Qualification;
    string NIC;

    Adult():Person()
    {
        Occupation = '\0';
        Qualification = '\0';
        NIC = '\0';
    }
    Adult(string Occupation, string Qualification, string NIC, string Name, int Age, string Gender) : Person(Name, Age, Gender)
    {
        this->Occupation = Occupation;
        this->NIC = NIC;
        this->Qualification = Qualification;
    }
    void setdata()
    {
        Person::setdata();
        string occupation, qualification/*, name, gender,*/ ,cnic;
           /* int age;*/
            cin.ignore();
           /* cout << " enter Name :" ;
            getline(cin, name, '\n');
            this->Name = name;
            cout << endl;*/
            cout << "enter occupation ";
            getline(cin, occupation, '\n');
            this->Occupation = occupation;
            cout << endl;
            /*cout << " Enter gender ";
            getline(cin, gender, '\n');
            this->Gender = gender;
            cout << endl;*/
            cout << " enter qualification ";
            getline(cin, qualification, '\n');
            this->Qualification = qualification;
            cout << endl;
            cout << " Enter cnic ";
            getline(cin, cnic, '\n');
            this->NIC = cnic;
            cout << endl;
           /* cout << " Enter age ";
            cin >> age;
            this->Age = age;
            cout << endl;*/

    }
    void printinfo()
    {
        Person::printinfo();
        /*cout << " name "<<Name << endl;*/
        cout << " occupation " << Occupation << endl;
        /*cout << " Gender " << Gender << endl;*/
        cout << " Qualifcation " << Qualification << endl;
        /*cout << " Age " << Age << endl;
        cout << " Gender " << Gender << endl;*/

    }
};
class kid :public Person {
public:
    string B_form_number;

    kid() : Person()
    {
        B_form_number = '\0';
    }
    kid(string B_form_number, string Name, int Age, string Gender) :Person(Name, Age, Gender)
    {
        this->B_form_number = B_form_number;
    }
    void setdata()
    {

        Person::setdata();
        string bform;
        cout << " Enter B Form number ";
    /*  getline(cin, bform, '\n')*/;
    cin >> bform;
        this->B_form_number = bform;
        cout << endl;

    }
    void setbform()
    {
        string bform;
        cout << " Enter B Form number ";
        getline(cin, bform, '\n');

        this->B_form_number = bform;
        cout << endl;
    }
    void printinfo()
    {
        Person::printinfo();
        cout << " b form number " << B_form_number;
    }
};
class Bogie /*:public kid*/ {
    int Bogie_ID;
    Bogie* next;
    Person* Adults;
    Person* kids;
    string familyName;
    int adultcount;
    int kidcount;
public:
    Bogie(int id)
    {
        Bogie_ID = id;
        next = nullptr;
        Adults = nullptr;

        kids = nullptr;
        familyName = '\0';
        adultcount = 0;
        kidcount = 0;
    }
    void AddPassengers() // should add adults and kids information etc
    {

        string familyName;

        cout << "Enter family name " << endl;
        cin.ignore();
        getline(cin, familyName, '\n');
        this->familyName = familyName;

        bool condition = false;
        bool condition2 = false;
        while (condition == false) // runs until valid input for adults
        {
            cout << " How many adults in the  family " << endl;
            cin >> adultcount;
            if (adultcount >= 1 && adultcount <= 4)
            {
                condition = true;
            }
            else
            {
                cout << " Invalid input " << endl;
                cout << " We have seats for 4 adults " << endl;
                condition = false;
            }
        }
        while (condition2 == false) //runs until valid input for kids
        {
            cout << " How many kids in the  family " << endl;
            cin >> kidcount;
            if (kidcount >= 1 && kidcount <= 6)
            {
                condition2 = true;
            }
            else
            {
                cout << " Invalid input " << endl;
                cout << " We have seats for 6 kids " << endl;
                condition2 = false;
            }
        }

        this->Adults = new Adult[adultcount];

        for (int i = 0; i < adultcount; i++)
        {

            Adults[i].setdata();


        }
            }



    }
};

int main()
{
    Bogie a(1);
    a.AddPassengers();
    a.Print();


}

我相信您需要将AdultsPerson *更改为Adult *

当程序尝试访问Adults[1]

for (int i = 0; i < adultcount; i++)
{
    Adults[i].setdata();
}

它在数组中跳到sizeof(Person)前面,而数组中的对象是sizeof(Adult) ,这会导致您看到的异常。

所以我找到了合适的方法,即在成员中将名为成人和儿童的指针声明为 Person ** Adults; Person **kids 然后是成年人 = new Person * [adultcount];

    for (int i = 0; i < adultcount; i++)
    {


        adults[i] = new Adult;
        adults[i]->setdata();

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM