2016-02-13 37 views
-4

我想分配一些數組在我的函數,但它似乎像我的構造函數沒有被調用?不能分配區域;中止陷阱: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

那麼,這是一個甜蜜和詳盡的錯誤信息。你爲什麼不按照說明找出答案? ('尺寸= 10416984889535361024'看起來相當大乍一看BTW) –

+1

你的程序試圖'malloc' 10416984889535361024字節。這幾乎是1000萬TiB。 –

+1

閱讀[爲​​什麼'input.eof()'是壞](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong)。 –

回答

0

的格式線造成泄漏的。聲明新的對象數組時。你必須爲物體留出足夠的空間。在程序嘗試將setName和setAge放入尚未分配內存的數組中之前。

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

如果你正在尋找泄漏,那麼你可能想看看'座椅*座椅=新座椅(seatNumber,價格,新人(姓名,年齡));'。這個分配被泄漏了(唯一使用'seat'指針變量的是一行註釋掉的代碼。另外,如果getReserver(),setName()或setAge()泄漏一些其他的錯誤),任何人都無法幫助解決這個問題 - 你還沒有發佈任何這些函數的代碼。 –