2017-04-23 290 views
0

該程序的目標是模擬具有6個「醫生隊列」的醫療綜合體。我試着保持它足夠接近我已完成的Java版本(必須以3種語言進行)。此時,當我運行DequeuePatientsListPatients方法時,程序意外終止,沒有錯誤。我試過調試器,但eclipse忽略了所有的斷點。爲什麼它終止?程序意外終止C++

的ListPatients方法是所有遵循Driver類:

void ListPatients() { 

    int x, QueueChoice = 0; 
    bool exit = false;`` 

    while (!exit) { 
     for (x = 1; x <= MAX; x++) { 
      cout << x << ": " << Doctor[x - 1] << "\n"; 
     } // end-for 

     cout << "Choose a Queue to List From"; 
     cin >> QueueChoice; 

     if (OpenFlag[QueueChoice - 1] == true) { //open flag for each queue 
     int i = Complex[QueueChoice - 1]->GetSize();//Global array of queues 
     //cout <<i<<endl; 
  1. 結束在該循環,如果函數調用

    for (x = 1; x <= i; x++) { 
         Patient This = Complex[QueueChoice-1]->GetInfo(x); //Program Terminates here 
         cout << x<< ": " << endl;//This.ID_Number; 
          //<<Complex[QueueChoice - 1]->GetInfo(x + 1).PrintMe() 
        } // end-for 
    } // end-if 
    
    cout << "Press 1 to List Another Queue, press 2 to exit"; 
    cin >> x; 
    switch (x) { 
    case 1: 
        break; 
    case 2: 
        exit = true; 
        break; 
        }//switch 
    } // end-while 
    } // List Patients` 
    

隊列類的GetInfo和指定者():

/*Return Patient info from each Node*/ 
Patient Queue::GetInfo(int Pos) { 
    Node* ComplexArray= new Node[Length]; 
    ComplexArray = this->toArray(); 
    return ComplexArray[Pos - 1].Info; 
} 

// The toArray method 
Node* Queue::toArray() { 
    // Copy the information in each node to an array and return the array. 
    int x = Length; 
    Node ComplexArray[Length] ={} ; 
    Node* Current = new Node(); 
    Current = Rear;` 
    for (x = 0; x<Length;x++) { 
     ComplexArray[x] = Node(); 
     ComplexArray[x].Modify(Current); 
     Current = Current->Next; 
    } 
    return ComplexArray; 
} // End of toArray method 

修改方法節點:

void Node :: Modify(Node* ThisNode) 
{ 
    Info = ThisNode->Info; 
    Next = ThisNode->Next; 
} 
+0

什麼是'Complex'又是什麼'GetInfo'嗎?如果在調試器中運行它會發生什麼? – doctorlove

+0

QueueChoice將不會與您輸入的數字相同 – Jay

+0

Complex是一個由6個隊列類對象組成的數組,它們都包含節點。每個節點都包含一個Patient'Info'對象和一個指向下一個節點的指針。 GetInfo應返回每個節點中包含的Patient對象。 – user3412695

回答

2

如果這是基本爲零爲什麼你從x

for (x = 0; x<Length;x++) { 
    ComplexArray[x-1] = Node(); 
    ComplexArray[x - 1].Modify(Current); 
    Current = Current->Next; 
} 

標誤差減去1 ++在C/C硬盤崩潰。

+0

*下標錯誤是硬崩潰在C /C++.*真的嗎? – DeiDei

+0

5點隧道視覺我想,它已經得到糾正。錯誤仍然存​​在 – user3412695

0

問題是基本的,這是我聲明我的指針,以及更改toArray函數返回Patient指針(這可以視爲一個數組?)作爲與Node相關的,而不僅僅是到數組的第一個元素。

這導致Array返回一個節點指針,該指針持續指向自己的指針Next

Queue Complex[6] = Queue(); //in driver class Patient* ComplexArray[Length] = new Patient();//Queue Class GetInfo & toArray

我需要:

Queue* Complex = new Queue[MAX]; Patient* ComplexArray = new Patient[Length];

,並改變了這些功能:

Patient Queue::GetInfo(int Pos) { 
    Patient* ComplexArray = new Patient[Length]; 
    ComplexArray= toArray(); 

    return ComplexArray[Pos - 1]; 
} 

// Now returns Patient pointer of Node->Info to GetInfo 
Patient* Queue::toArray() { 
    Node* Current; 
    int x; 
    Patient* ComplexArray = new Patient[Length]; 
    Current = Rear; 
    for (x = 1; x <= Length; x++) { 
     ComplexArray[x-1] = Patient(); 
     ComplexArray[x - 1].Modify(Current->Info); 
     Current = Current->Next; 
    } 
    return ComplexArray; 
} // End of toArray method