我正在为我的学校作业写一个简单的航班预订系统。我应该在不确定大小的情况下动态创建数组。因为我必须跟踪数组的大小,所以我在类中声明了一个名为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,我可能会非常错误。任何帮助将不胜感激。
答案 0 :(得分:0)
flightPlan
中的Flight
呢?
我看到它是在构造函数中分配的
flightPlan = new char*[seatNo];
并将其复制到复制构造函数
中char** flightPlan = obj.flightPlan;
它是在析构函数中删除的吗?
如果是这样,当您将Flight
从旧数组转换为新数组时(有很多更好的方法可以做到,但我现在正在寻找您的错误)< / p>
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];
}
在新的(复制的)Flight
中,指向flightPlan
的指针指向已删除的区域。使用它们时,请确保EXC_BAD_ACCESS。
p.s。:额外建议:尽可能避免直接使用分配的内存;考虑使用std::vector<Fligth>
listOfFlights
。
答案 1 :(得分: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
而不是手动管理的数组,可以轻松避免此处的每个错误。
答案 2 :(得分:0)
谢谢大家的回答。