简体   繁体   English

程序意外终止C ++

[英]Program Terminates unexpectedly C++

The goal of the program is to simulate a medical complex with 6 "Doctor Queues". 该程序的目标是用6个“ Doctor Queues”模拟一个医疗综合体。 I tried keeping it close enough to the Java version I've completed (must do this in 3 languages). 我尝试使其与已完成的Java版本保持足够的距离(必须使用3种语言来完成此操作)。 At this point when I run the DequeuePatients and ListPatients method, the program terminates unexpectedly with no errors. 此时,当我运行DequeuePatientsListPatients方法时,程序意外终止,没有错误。 I've tried the debugger but eclipse is ignoring all my breakpoints.Why is it terminating? 我已经尝试过调试器,但是eclipse忽略了我所有的断点,为什么会终止?

The ListPatients method is all follows in Driver class: 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. Terminates in this loop if function is called 如果调用了函数,则在此循环中终止

      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` 

Queue Class GetInfo and toArray(): 队列类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

Modify Method in Node: 节点中的修改方法:

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

If this is base-zero why are you subtracting 1 from x 如果这是零基,为什么要从x减去1

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

Subscript errors are hard crashes in C/C++. 下标错误是C / C ++中的严重崩溃。

The problem was sort of basic, it was way I was declaring my pointers, as well as changing the toArray function to return a Patient pointer(That can be treated as an array?) as apposed to Node, and not just to the first element of the array. 问题是基本的,这是我声明指针的方式,以及更改toArray函数以将Patient指针(可以视为数组?)返回到Node的方式,而不仅仅是到第一个元素的方式的数组。

this caused toArray to return one Node pointer, with that instances Next pointer continuously pointing to itself. 这导致toArray返回一个Node指针,该实例的Next指针连续指向自身。

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

I needed: 我需要:

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

and changed these functions: 并更改了以下功能:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM