【发布时间】:2011-10-07 11:58:20
【问题描述】:
我正在尝试制作一个模拟器类,该类将使用队列将车厢从火车添加到队列中,因此它将成为一个车厢队列。 Carriage 也是 train 类的 dataType。
我将使用一种方法在火车到达时将它们排入队列,并将它们添加到不同的队列中。一旦他们的货物被卸载,我也会有一种方法让火车出队。
我有一个 Train 类,它有一个私有变量:
LinkedList<Carriage> *list;
队列类有一个私有变量:
LinkedList<dataType> *list;
在我正在调用的演示文件中:
Train* train1; //declare a train.
train1 = new Train(arr); //instantiate train with an array of integers
Queue<Carriage> queue1; //declare a queue.
queue1 = new Queue<Carriage>(train1); //instantiate queue with train data.
我的队列类有问题,我不太确定如何实现它。
我的队列类.h:
template <typename dataType> //dataType
class Queue
{
public:
Queue();
Queue(dataType arr[]);
~Queue();
void pop();
void push(dataType data);
private:
LinkedList<dataType> *list;
};
#include "Queue.template"
//there is also a namespace and a macroguard, left them out of this.
编辑 - 没有添加足够的信息。
在上面的演示文件代码中,我希望能够调用 queue1 = new Queue(train1);
当我这样做时,我得到了错误,所以我知道我的构造函数做错了,因为它们都是链表,我是否需要使用循环将车厢分配到队列中?
我需要帮助的是让火车车厢进入队列。
谢谢:)
【问题讨论】:
-
你需要让你的问题更具体。 “我不确定如何实施”不是很具体。你试过什么?你想要什么样的建议?
-
元评论:你不认为队列是一列火车的错误抽象。想想您可以在队列上执行的操作与您可以在火车车厢上执行的操作。例如,如何在火车中间插入车厢?
-
部分模拟使用了卸货设施,在该设施中处理火车并移除其负载,这就是火车将进入队列的原因。
标签: c++ queue linked-list simulator