【问题标题】:Program Terminates unexpectedly C++程序意外终止 C++
【发布时间】:2017-04-23 09:09:28
【问题描述】:

该程序的目标是模拟具有 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`
    

队列类 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;
 }

【问题讨论】:

  • Complex 是什么,GetInfo 有什么作用?如果在调试器中运行它会发生什么?
  • QueueChoice 不会等于您输入的数字
  • Complex 是一个包含 6 个 Queue 类对象的数组,所有对象都包含节点。每个节点都包含一个 Patient Info 对象和一个指向下一个节点的指针。 GetInfo 应该返回每个 Node 中包含的 Patient 对象。
  • GetInfo 调用具有x=0toArray 并设置ComplexArray[x-1] 将是0-1 开始。
  • ...版本控制对这类事情很有帮助

标签: c++ eclipse pointers queue


【解决方案1】:

如果这是基数为零,为什么要从 x 中减去 1

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

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

【讨论】:

  • 下标错误是 C/C++ 中的硬崩溃。真的吗?
  • 早上 5 点隧道视觉我想,它已得到纠正。错误仍然存​​在
【解决方案2】:

问题有点基本,这是我声明指针的方式,以及更改 toArray 函数以返回与 Node 相对的 Patient 指针(可以视为数组?),而不是只是到数组的第一个元素。

这导致 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-11
    • 2021-08-24
    • 2014-02-22
    • 2013-07-19
    • 1970-01-01
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多