这是我在Airport.cpp的主要内容:
#include <iostream>
#include "Airport_Queue.h"
#include "Airplane.h"
#include <stdlib.h>
int main(){
Airplane *b = new Airplane(true);
(*b).come();
(*b).go();
std::cout << "........." << std::endl;
Airport_Queue *landing_queue = new Airport_Queue(5);
Airplane *a0 = new Airplane(true);
(*landing_queue).enqueue(a0); //error here
//(*landing_queue).dequeue();
return 0;
这是我的Airport_Queue.cpp
#include "Airport_Queue.h"
Airport_Queue::Airport_Queue(unsigned n){
Airplane** a = new Airplane*[n];
capacity = n;
size = 0;
head = tail = 0;
}
Airport_Queue::~Airport_Queue(){
for (size_t i = 0; i < size; i++){
delete a[i];
}
delete [] a;
}
void Airport_Queue::enqueue(Airplane* airplane){
if (!(*this).isFull()){
a[tail] = airplane;
(*a[tail]).come();
tail = (tail+1) % capacity;
size++;
}
else{
std::cerr << "Queue is full." << std::endl;
}
}
Airplane* Airport_Queue::dequeue(){
if (!(*this).isEmpty()){
size_t x = head;
(*a[head]).go();
head = (head+1) % capacity;
size--;
return a[x];
}
else{
std::cerr << "Queue is empty." << std::endl;
return NULL;
}
}
bool Airport_Queue::isEmpty(){
if (size == 0)
return true;
else
return false;
}
bool Airport_Queue::isFull(){
if (size == capacity)
return true;
else
return false;
}
int Airport_Queue::getSize(){
return size;
}
我还有一个名为Airplane的课程。我用来编译和链接的命令是
g++ -std=c+11 -Wall -g -o airport Airport.cpp Airplane.cpp Airport_Queue.cpp
如何修复此运行时错误?错误是在我拨打enqueue
时。然后我得到
4 [main] airport 3796 cygwin_exception :: open_stackdumpfile:将堆栈跟踪转储到airport.exe.stackdump
请。谢谢你
答案 0 :(得分:0)
我看到的问题:
您有一个成员变量size
。您需要两个 - capacity
和size
。
在Airport_Queue::Airport_Queue
Airport_Queue::Airport_Queue(unsigned size){
Airplane** a = new Airplane*[size];
size = 0; // This is the argument, not the member variable.
// The member variable remains uninitialized.
head = tail = 0;
}
你需要:
Airport_Queue::Airport_Queue(unsigned size){
Airplane** a = new Airplane*[size];
this->capacity = size;
this->size = 0;
head = tail = 0;
}
在Airport_Queue::~Airport_Queue
Airport_Queue::~Airport_Queue(){
for (size_t i = 0; i < sizeof(a); i++){
// sizeof(a) does not give you the number of elements
// in the array. It just gives you the size of the pointer.
delete a[i];
}
delete [] a;
}
你需要:
Airport_Queue::~Airport_Queue(){
for (size_t i = 0; i < this->size; i++){
delete a[i];
}
delete [] a;
}
在Airport_Queue::enqueue
遵循析构函数的逻辑,需要更改以下行。
tail = (tail+1) % sizeof(a);
将其更改为:
tail = (tail+1) % capacity;
In In Airport_Queue::dequeue
您的错误与上述类似。改变行
head = (head+1) % sizeof(a);
到
head = (head+1) % capacity;
在Airport_Queue::isFull
更改行
if (size == sizeof(a))
到
if (size == capacity)
我希望这可以解决你的大部分问题。如果有任何我错过的,希望你能找到它们。