如何通过队列指针访问存储在节点中的类中的数据?

时间:2017-11-13 23:38:54

标签: c++ class data-structures queue

在我目前的任务中,我无法弄清楚如何访问这一特定数据。

要开始,分配要求从文件中提取数据以模拟正常运行的商店。唯一的问题是,提取的数据是关于客户的。特别是当客户输入队列以及收银员处理订单需要多长时间时。

现在我如何将客户数据存储在一个类数组中。

for(int i = 0; i < entries; i++)
{  
  /* !!!!!IMPORTANT!!!!!  
   * The way this program extracts file data assumes that the file will  
follow a specific format  
   * results will vary heavily based on how the file is set up  
   *   
   * before docking points please make sure that the file follows the format  
   *   
   *  "Number of entries"  
   *  "Customer Number", "Arrival Time", "Service Time"  
   *  "Customer Number", "Arrival Time", "Service Time"  
   *  "Customer Number", "Arrival Time", "Service Time"   
   */

  xfiles >> dataEntry;
  fileData[i].setNumber(dataEntry);
  //inserts a number from the file into the "number" variable in the customer class.
  xfiles >> dataEntry;
  fileData[i].setArrival(dataEntry);
  //inserts a number from the file into the "arrival" variable in the customer class.
  xfiles >> dataEntry;
  fileData[i].setServTime(dataEntry);
  //inserts a number from the file into the "servTime" variable in the customer class.
} 

xfiles.close();

它不包含在代码中,但有一行代表程序中较早的条目。

在我的下一个区块中,我必须在一段时间内同时排队和处理客户。我知道如何排队他们,但我不太清楚我应该如何处理它们。对于我现在所知道的,我相信我可能想要做一个条件语句来检查阵列中的某个客户是否已经排队。

我目前正在尝试访问的数据是存储在课堂中的到达时间。

所以,像 FILEDATA [I] .returnArrival();

但由于该类存储在队列中,我不知道如何能够访问它。

现在我排队的是

for(int x = 0; x < 570; x++)
{
  if(cusTime == x)
  {
     if(scully.isFull() = false)
        scully.enqueue(fileData[cusTime]);
     else if(mulder.isFull() = false)
        mulder.enqueue(fileData[cusTime]);
     else if(skinner.isFull() = false)
        skinner.enqueue(fileData[cusTime]);
     else
        cout << "queues are full, disposing..\n";
  }
  cusTime++;
}

起初我觉得它会像

scully.returnFront()->temp->returnClass()->fileData.returnArrival();  

但我不确定,因为temp只是在队列类中声明的指针。

我的一位朋友提出了另一个建议,建议它可能是这样的,但是当我运行代码时,我最终得到了分段错误。

scully.returnFront()->returnClass().returnArrival();

2 个答案:

答案 0 :(得分:1)

我认为应该如下:

scully.returnFront().returnArrival()

因为您将数组中的项排入队列。因此,returnFront()检索应该可以使用方法的项目。

答案 1 :(得分:0)

在与教授和TA讨论之后,问题的原因是返回前端函数返回指针使得访问节点内的数据变得更加困难。一个解决方案是让返回前端函数返回与数据相关联的Class,并让return语句返回一个指针,该指针指向返回存储在节点中的类的类函数。

所以

Node *returnFront();

已更改为

Customer returnFront();

功能内的变化是

return front;

return front->returnClass();

这些更改使得从主文件内部访问Customer类数据变得更加容易。所以我能够为该类实例化一个保存变量的新位置。

Customer scullyTemp;

之后,通过赋值语句存储从节点中存储的类中的数据。

scullyTemp = scully.returnFront();
scullyTemp.returnArrival();

它可能比它需要的要复杂一点,但是现在它做了我需要做的事情。