2016-02-13 8 views
-4

Я пытаюсь выделить некоторые массивы в своей функции, но похоже, что мои конструкторы не вызываются?Невозможно выделить регион; abort trap: 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 
---------- ---------- 
+2

Хорошо, что это такое сладкое и сложное сообщение об ошибке. Почему бы вам просто не следовать инструкциям, чтобы узнать? ('size = 10416984889535361024' выглядит довольно большим на первый взгляд BTW) –

+1

Ваша программа пыталась« malloc »10416984889535361024 байт. Это почти 10 миллионов TiB. –

+1

Читать [Почему 'input.eof()' is bad] (http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). –

ответ

0

линии являются те, вызывающие утечку. При объявлении новых массивов объектов. вы должны сделать достаточно места для объекта. Перед тем, как программа пыталась установитьName и setAge в массиве, который еще не был выделен памятью.

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

Если вы ищете утечку, вам может потребоваться посмотреть «Сиденье» = новое сиденье (seatNumber, цена, новое лицо (имя, возраст)); '. Это распределение получает утечку (единственное, что использует переменную указателя 'seat', - это строка кода, которая закомментирована. Также, если' getReserver() ',' setName() 'или' setAge() 'утечка (или какая-то другая ошибка), никто не может помочь с этим - вы не разместили код для какой-либо из этих функций. –