把类和实现分开写,再写一个主函数,编译无错,但运行出现问题。将头文件后缀改后这也是从CSDN搜到的解决方法,不知道具体原因,求问。代码列下queue.htemplate<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;
}
1 回答
已采纳
AAnonymous
TA贡献62条经验 获得超31个赞
因为.h中只是做了声明,可能编译时VS也没有把中间lib编译进来,导致你实例化时需要调用构造函数,它找不到构造函数的实现。#include cpp时就把实现包含进来了
可以参考这个文章:"undefined reference to" 问题汇总及解决方法
- 1 回答
- 0 关注
- 1240 浏览
添加回答
举报
0/150
提交
取消