程序意外终止C ++

时间:2017-04-23 09:09:29

标签: c++ eclipse pointers queue

该计划的目标是用6“医生队列”模拟医疗综合体。我试着把它保持足够接近我已经完成的Java版本(必须用3种语言来做)。此时,当我运行DequeuePatientsListPatients方法时,程序意外终止,没有错误。我已经尝试过调试器,但eclipse忽略了我的所有断点。为什么它会终止?

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`
    
  2. 队列类GetInfo和toArray():

    /*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;
     }
    

2 个答案:

答案 0 :(得分:2)

如果这是零基数,为什么要从x

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

下标错误是C / C ++中的硬崩溃。

答案 1 :(得分:0)

问题是基本的,我正在宣布我的指针,以及更改toArray函数以返回一个患者指针(可被视为一个数组?)与Node相关的方式,而不仅仅是数组的第一个元素。

这导致toArray返回一个Node指针,实例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
相关问题