把类和实现分开写,再写一个主函数,编译无错,但运行出现问题。
将头文件后缀改后
这也是从CSDN搜到的解决方法,不知道具体原因,求问。
代码列下
queue.h
template<class T> class Queue { public: virtual bool IsEmpty() const = 0; virtual bool IsFull() const = 0; virtual bool Front(T& x) const = 0; virtual bool EnQueue(T x) = 0; virtual bool DeQueue() = 0; virtual void Clear() = 0; };
SetQueue.h
#include"queue.h" template<class T> class SeqQueue : public Queue<T> { public: SeqQueue(int mSize); ~SeqQueue() { delete[] q; } bool IsEmpty() const { return front == rear; } bool IsFull() const { return (rear + 1) % maxSize == front; } bool Front(T& x) const; bool EnQueue(T x); bool DeQueue(); void Clear() { front = rear = 0; } private: int front, rear; int maxSize; T *q; };
SetQueue.cpp
#include"SetQueue.h" template<class T> SeqQueue<T>::SeqQueue(int mSize) { maxSize = mSize; q = new T[maxSize]; front = rear = 0; } template<class T> bool SeqQueue<T>::Front(T & x) const { if (IsEmpty()) { cout << "empty" << endl; return false; } x = q[(front + 1) % maxSize]; return true; } template<class T> bool SeqQueue<T>::EnQueue(T x) { if (IsFull()) { cout << "Full" << endl; return false; } q[(rear = (rear + 1) % maxSize)] = x; return true; } template<class T> bool SeqQueue<T>::DeQueue() { if (IsEmpty()) { cout << "Underflow" << endl; return false; } front = (front + 1) % maxSize; return true; }
AAnonymous
相关分类