2016-11-30 138 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

对于问这样一个愚蠢的问题,我表示歉意。问题终于解决了。显然,删除刚刚复制的指针是不好的,会在代码中造成问题。

谢谢大家的回答。