2015-02-24 32 views
1

這裏是我的主要在Airport.cpp:C++/G ++ cygwin_exception :: open_stackdumpfile:轉儲堆棧跟蹤* .exe.stackdump

#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; 
} 

我也有一個叫飛機的課。我用來編譯和鏈接的命令是 g++ -std=c+11 -Wall -g -o airport Airport.cpp Airplane.cpp Airport_Queue.cpp 我該如何解決這個運行時錯誤?錯誤是當我打電話enqueue。然後我得到

4 [主]機場3796 cygwin_exception :: open_stackdumpfile:轉儲堆棧跟蹤airport.exe.stackdump

請。謝謝你。

+0

不要這樣做到你的文章。當你閱讀我的答案時,它將不再有意義。你應該做的是(1)保留原始代碼,然後在底部添加更新的代碼,或者(2)用更新後的代碼創建一個全新的帖子。 – 2015-02-24 05:15:16

回答

0

問題,我看到:

  1. 你有一個成員變量size。你需要兩個 - capacitysize

  2. 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; 
    } 
    
  3. 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; 
    } 
  1. Airport_Queue::enqueue

    繼從析構函數的邏輯,以下行需要被改變。

    tail = (tail+1) % sizeof(a); 
    

    將其更改爲:

    tail = (tail+1) % capacity; 
    
  2. 在在Airport_Queue::dequeue

    您有類似上述的錯誤。更改線路

    head = (head+1) % sizeof(a); 
    

    head = (head+1) % capacity; 
    
  3. Airport_Queue::isFull

    更改線路

    if (size == sizeof(a)) 
    

    if (size == capacity) 
    

我希望這可以解決您的大部分問題。如果有任何我錯過了,希望你能找到它們。

+0

謝謝。我修正了所有這些,但仍然有相同的錯誤。我將更新帖子中的代碼,以便您可以看到。 – Myath 2015-02-24 05:04:07