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

时间:2016-02-13 21:28:28

标签: c++ memory-management abort

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

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
---------- ----------

1 个答案:

答案 0 :(得分:0)

线条是造成泄漏的线条。声明新对象数组时。你必须为对象留出足够的空间。在程序尝试将setName和setAge放入尚未分配内存的数组之前。

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