无法分配区域;中止陷阱:6 编译器错误



我正在尝试在我的函数中分配一些数组,但似乎没有调用我的构造函数?

void Ticket::load(ifstream & inputFile)
{
string a;
int ticketCount = -1;
int seatCount;
Ticket tickets[2];
// try to open the waitlist.txt file as input mode
//inputFile.open("hi.txt");
inputFile.open("hi.txt");

// cannot open the file
if (!inputFile.is_open())
    cout << "Cannot open the file.n";
// open successfuly
else
{
    string category = "";
    string data = "";
    string name = ""; 
    string flightCode = "";
    int age;
    int seatNumber;
    int c;
    double d;
    double price = 0;
    inputFile >> category;
    inputFile >> data;
    // keep reading till the end of the file
    while (!inputFile.eof())
    {
        if (category == "Ticket:")
        {
            /*
            * Here we increment the index of the ticket in the array.
            * But in our actual Airplane Project we will use LinkedList
            * to store the Tickets, so we will dynamically allocate
            * memory whenever we read the word "Ticket:" in the file.
            * Other than that, the way of reading the file will be the same.
            */
            c = stoi(data);
                cout << "Ticket Number:"<< c <<endl;
            //Ticket::setTicket(tickets, c);
            tickets[++ticketCount].ticketNumber = c;
            seatCount = 0;
                }
        else if (category == "Size:")
        {

            c = stoi(data);
            tickets[ticketCount].groupSize = c;
            // allocate space for the seat array
            tickets[ticketCount].seatInfo = new Seat [c];
            tickets[ticketCount].seatInfo->reserver = new Person[c];

        }
        /*else if (category == "Flight:")
        {
            flightCode = data;
        }
        */
        else if (category == "Name:")
        {
            name = data;
                            /*
            * keep reading data for name because it may contain white spaces
            * stop whenever the data is "Age:" (new category)
            */
            inputFile >> data;
            while (data != "Age:")
            {
                name += " " + data;
                inputFile >> data;

            }
            /*
            * done reading the name
            * set the category to be the current data,
            * then go to the READ_DATA label to read in the new data
            */
            category = data;
            goto READ_DATA;
        }
        else if (category == "Age:")
        {
            age = stoi(data);
        }
        else if (category == "Seat:")
            seatNumber = stoi(data);
        else if (category == "Price:")
        {
            d = stod(data);
            price = d;
            // fill the seat in the array
                        tickets[ticketCount].seatInfo[seatCount].reserver[seatCount].name = name;
                tickets[ticketCount].seatInfo[seatCount].reserver[seatCount].age = age;
            tickets[ticketCount].seatInfo[seatCount].seatNumber =     seatNumber;
            tickets[ticketCount].seatInfo[seatCount++].price = price;
        }
        inputFile >> category;
    READ_DATA:  // label to jump to
        inputFile >> data;
    }
    // close the file after finish reading
    inputFile.close();

}
}

当我到达底部时,我收到一个索引越界错误,我尝试将它们添加到我的工单列表中。我似乎无法在seatCount = 2;添加任何内容.

            tickets[ticketCount].seatInfo = new Seat [c];
            tickets[ticketCount].seatInfo->reserver = new Person[c];

这是我输入文件的格式

Ticket: 01
Size: 1
Name: Namees
Age: 20
Seat: 34
Price: 100
---------- ----------
Ticket: 02
Size: 3
Name: Poop master
Age: 20
Seat: 23
Price: 100
Name: Gun Master
Age: 19
Seat: 21
Price: 100
Name: idccc
Age: 21
Seat: 22
Price: 100
---------- ----------
这些

行是导致泄漏的行。声明新对象数组时。必须为对象腾出足够的空间。在程序尝试在尚未分配内存的数组中设置名称和 setAge 之前。

tickets[ticketCount].seatInfo[seatCount].getReserver().setName(name);
tickets[ticketCount].seatInfo[seatCount].getReserver().setAge(age);

最新更新