2016-11-30 96 views
0

我正在爲我的學校作業寫一個簡單的航班預定系統。我應該動態創建一個數組而不確定大小。由於我必須跟蹤數組的大小,所以我在我的類中聲明瞭一個名爲count的整型變量。我也有一個飛行班,有一個複製構造函數和一對getter。然後,我寫了下面的方法動態分配陣列

void ReservationSystem::addFlight(const int flightNo, const int rowNo, const int seatNo) { 
    if (count == 0) { 
     Flight *tmp = new Flight(flightNo, rowNo, seatNo); 
     listOfFlights = new Flight*[count+1]; 
     listOfFlights[count] = tmp; 
     count++; 
    } else { 
     bool check = true; 
     for (int i = 0; i < count && check; i++) { 
      if (listOfFlights[i]->getFlightNo() == flightNo) { 
       std::cout << "There is already a flight with that flight code" << std::endl; 
       check = false; 
      } 
     } 

     if (check) { 
      Flight *tmp = new Flight(flightNo, rowNo, seatNo); 
      Flight** tmparr = new Flight*[count + 1]; 

      for (int i = 0; i < count; i++) { 
       Flight *f = new Flight(*listOfFlights[i]); 
       tmparr[i] = f; 
      } 

      tmparr[count + 1] = tmp; 

      for (int i = 0; i < count; i++) { 
       delete listOfFlights[i]; 
      } 

      delete listOfFlights; 
      listOfFlights = tmparr; 
      count++; 
     } 


    } 

} 

我也有一個showFlight(const int flightCode)方法表示具體的航班:

void ReservationSystem::showFlight(const int flightNo) { 
    bool check = true; 
    for (int i = 0; i < count; i++) { 
     if (listOfFlights[i]->getFlightNo() == flightNo) { 
      std::cout << "Flight " << listOfFlights[i]->getFlightNo() << " has " << listOfFlights[i]->getAvailableSeats() << " available seats" << std::endl; 
      listOfFlights[i]->printSeats(); 
      check = false; 
     } 
    } 
} 

這是我的默認構造函數和拷貝構造函數Flight類:

Flight::Flight(const int flightNo, const int rowNo, const int seatNo) { 
    flight = flightNo; 
    row = rowNo; 
    seat = seatNo; 
    available = rowNo * seatNo; 
    flightPlan = new char*[seatNo]; 

    // initialize the flight plan to all seats available 
    for(int i = 0; i < seatNo; ++i) flightPlan[i] = new char[rowNo]; 

    for(int i = 0; i < seatNo; ++i) { 
     for(int j = 0; j < rowNo; ++j) flightPlan[i][j] = 'o'; 
    } 
} 

Flight::Flight(const Flight &obj) { 
    const int flight = obj.flight; 
    const int row = obj.row; 
    const int available = obj.available; 
    char** flightPlan = obj.flightPlan; 

} 

但在行if (listOfFlights[i]->getFlightNo() == flightNo) xcode給我EXC_BAD_ACCESS錯誤。我認爲背後的原因是我的addFlight()方法出現故障,因爲沒有對象,數組指向null,對吧?由於它不能達到getFlightNo()方法,它會引發此錯誤。

請注意,這是我第一次使用C++,所以我是一個完整的n00b,而且我可能會非常錯誤。任何幫助將不勝感激。

+2

該複製構造函數看起來確實是錯誤的,只是設置了一堆未使用的局部變量。 –

+0

爲什麼不使用'std ::'容器類?像'std :: vector <>'? – nvoigt

+1

如果你的學校教你數組和指針數組,指向數組和'new []',以及'std :: vector'之前的所有這些,他們會把你扯掉。 –

回答

0

flightPlanFlight

我看到在構造函數中

flightPlan = new char*[seatNo]; 

和在拷貝構造函數

char** flightPlan = obj.flightPlan; 

它的析構函數刪除複製分配呢?

如果是這樣,當你翻譯從舊陣列到新數組一個Flight應對它(裏面有很多更好的方法來做到這一點,但我在尋找你的錯誤現在)

 Flight** tmparr = new Flight*[count + 1]; 

     for (int i = 0; i < count; i++) { 
      Flight *f = new Flight(*listOfFlights[i]); 
      tmparr[i] = f; 
     } 

     // ... 

     for (int i = 0; i < count; i++) { 
      delete listOfFlights[i]; 
     } 

in new(copied)Flight s指向flightPlan的指針指向已刪除區域。當你使用它們時,確信EXC_BAD_ACCESS。

小號:額外的建議:儘可能避免直接使用分配的內存;考慮使用std::vector<Fligth>代替listOfFlights

0

問題1:

  Flight *tmp = new Flight(flightNo, rowNo, seatNo); 
      Flight** tmparr = new Flight*[count + 1]; 

      for (int i = 0; i < count; i++) { 
      // **** 
      // **** You are trying to copy a flight. Why? You have an array of 
      // **** Flight pointers. You need to make a room for one more poniter. 
      // **** You can continue pointing at the same flights as before. 
      // **** You don't need an array of brand new flights. 
      // **** 
       Flight *f = new Flight(*listOfFlights[i]); // <---- WRONG 
       Flight *f = listOfFlights[i]; // <------------------ BETTER 
       tmparr[i] = f; 
      } 

     // **** This is not needed any more 
     // **** 
     for (int i = 0; i < count; i++) { 
      delete listOfFlights[i]; 
     } 

問題2:

  // **** 
      // **** Learn to count. There are count+1 elements in your new array. 
      // **** The first index is 0. The last index is count, not count+1. 
      // **** You did get it right in the count==0 case. 
      // **** 
      tmparr[count + 1] = tmp; //<--- WRONG 
      tmparr[count] = tmp; // <------ RIGHT 

問題3

// **** 
// **** This copies nothing. It just creates and then forgets a bunch of local variables. 
// **** You probably don't need a copy constructor at all because 
// **** you don't need to copy flight any more. 

Flight::Flight(const Flight &obj) { 
    const int flight = obj.flight; 
    const int row = obj.row; 
    const int available = obj.available; 
    char** flightPlan = obj.flightPlan;  
} 

// **** Declare the copy constructor deleted instead. 

Flight::Flight(const Flight &) = delete; 

// **** If they are making you to use an old compiler that doesn't understand delete here 
// **** Then do this instead: 

private: Flight(const Flight &); // no implementation 

// **** You may want to do the same with the assignment operator 

// **** If you think do need a copy constructor for some reason, think again. 

// **** OK, if you still think you need a copy constructor, 
// **** make sure you DO NOT do this: 

flightPlan = obj.flightPlan 

// **** or an equivalent. You need a brand new copy of all of your arrays. 
// **** Also maje sure to define a copy assignment operator. 
// **** regardless, you need a destructor. Make sure you delete the arrays there. 

注意,這裏這裏的每一個錯誤能夠被輕鬆地通過使用std::vector,而不是迴避手動管理陣列。

0

對於問這樣一個愚蠢的問題,我表示歉意。問題終於解決了。顯然,刪除剛剛複製的指針是不好的,會在代碼中造成問題。

謝謝大家的回答。