如何通过队列指针访问存储在节点中的类中的数据
How do I access data in a class stored in a node through queue pointers?
在我当前的任务中,我很难弄清楚如何访问这段特定的数据。
首先,分配要求从文件中提取数据,以模拟正常运行的商店。唯一的问题是,所提取的数据是关于客户的。具体来说,就是客户何时进入队列,以及收银员处理订单需要多长时间。
现在我如何将客户数据存储在一组类中。
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();
我认为应该是以下内容:
scully.returnFront().returnArrival()
因为您将数组中的项目排入队列。因此,returnFront()检索一个项目,您的方法应该可以在该项目上使用。
与教授和TA讨论后,问题的原因是return front函数返回了一个指针,使访问节点内的数据变得更加困难。一个解决方案是让return front函数返回与数据关联的Class,并让return语句返回一个指针,该指针指向返回存储在节点中的类的类函数。
所以
Node *returnFront();
已更改为
Customer returnFront();
功能内的变化是
return front;
至
return front->returnClass();
这些更改使从主文件内部访问Customer类数据变得更加容易。因此,我能够为类实例化一个新的占位符变量。
Customer scullyTemp;
然后存储类内部的数据,这些数据通过赋值语句存储在节点中。
scullyTemp = scully.returnFront();
scullyTemp.returnArrival();
它可能比它需要的要复杂一点,但现在它做了我需要它做的
- 如何使用发送数据包所花费的时间计算两个节点之间的距离?
- 如何在Visual C+++中从JSON文件中读取节点数据
- 修改链表主函数代码,用户将在其中输入节点的索引和数据以及正确的消息
- 如何在C++中实现节点数任意的通用树数据结构?
- 如何在C++中使用带有SFML的http reqest从节点.js服务器获取数据?
- 如果我们不创建一个新节点并使用指针插入数据并建立链接(在链表中)怎么办?
- 使用 Broadcast 发出的从节点服务器发送的数据不能被 C++ 套接字 IO 客户端读取
- C++ Eclipse:二叉搜索树节点>数据=变量似乎不起作用
- C++ 在节点内存储对数据对象的引用,而不复制数据
- 如何通过其持有的数据从链表中删除某个节点?
- 基于给定字符串数据类型的链表删除节点
- 为什么我的节点临时节点不能显示正确的数据?
- C++中的数据结构,插入链表中节点的开头
- 从链表数组访问节点数据
- 如何取消引用作为指针的链表节点数据
- 如何将XML节点数据转换为QT中的字符串
- 无法访问类中的节点数据
- C++ 使用 std::p air 模板专用化定义树节点数据结构
- 如何使用pugixml解析获得具有相同节点名称的节点的节点数据
- 如何添加备用节点数据