程序意外终止C

Program Terminates unexpectedly C++

本文关键字:终止 意外 程序      更新时间:2023-10-16

该程序的目的是模拟用6个"医生队列"的医学综合体。我试图将其保持足够近的距离与我完成的Java版本(必须用3种语言执行此操作(。此时,当我运行DequeuePatientsListPatients方法时,程序出乎意料地终止而没有错误。我尝试了调试器,但是Eclipse忽略了我所有的断点。为什么它终止?

listApatients方法在驱动程序类中均为:

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

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

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

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

问题是基本的,这是我宣布指针的方式,以及更改toArray功能以返回患者指针(可以将其视为数组?(插入节点,而不仅仅是数组的第一个元素。

这导致Toarray返回一个节点指针,该实例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