大家帮我看看这个程序呀。我调了半天了,还是链接出错。到底哪里错了???
这是头文件:#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>
using namespace std;
template <typename Type>
class QueueItem;
template <typename Type>
class Queue
{
friend ostream &operator <<(ostream &, const Queue<Type> &);
public:
Queue();
~Queue();
Type remove();
void add(const Type &);
bool is_empty() const;
private:
QueueItem<Type> *front;
QueueItem<Type> *back;
};
template <typename Type>
inline Queue<Type>::Queue()
:front(0),back(0)
{}
template <typename Type>
inline Queue<Type>::~Queue()
{
while(!is_empty())
remove();
}
template <typename Type>
inline bool Queue<Type>::is_empty() const
{
return front == 0;
}
template <typename Type>
inline void Queue<Type>::add(const Type &val)
{
QueueItem<Type> *pt = new QueueItem<Type>(val);
if(is_empty())
front = back = pt;
else
{
back->next = pt;
back = pt;
}
back->next = 0;
}
template <typename Type>
inline Type Queue<Type>::remove()
{
if(is_empty())
{
cerr << "remove() on empty queue!" << endl;
exit(-1);
}
QueueItem<Type> *pt = front;
front = front->next;
Type resval = pt->item;
delete pt;
return resval;
}
template <typename Type>
class QueueItem
{
friend class Queue<Type>;
friend ostream &operator <<(ostream &, const QueueItem<Type> &);
public:
QueueItem(const Type &ie):item(ie) {}
private:
Type item;
QueueItem *next;
};
#endif
这是主函数:
#include "Queue.h"
#include <iostream>
using namespace std;
template <typename Type>
ostream &operator <<(ostream &os, const QueueItem<Type> &q)
{
os << q.item;
return os;
}
template <typename Type>
ostream &operator <<(ostream &os, const Queue<Type> &q)
{
os << "<";
QueueItem<Type> *p;
for(p=q.front;p;p=p->next)
os << *p << " ";
os << ">";
return os;
}
int main()
{
/*Queue<int> *p_qi = new Queue<int>;
int ival;
for(ival = 0;ival < 10;++ival)
p_qi->add(ival);
int errcnt = 0;
for(ival = 0;ival < 10;++ival)
{
int qval = p_qi->remove();
if(ival != qval)
++errcnt;
}
if ( !errcnt )
cout << "!! queue executed ok\n";
else
cerr << "?? queue errors: " << errcnt << endl;*/
Queue<int> qi;
cout << qi << endl;
int ival;
for(ival=0;ival<10;++ival)
qi.add(ival);
//cout << qi << endl;
return 0;
}
错误显示为:
错误 1 error LNK2019: 无法解析的外部符号 "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Queue<int> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$Queue@H@@@Z),该符号在函数 _main 中被引用 Queue.obj CP16
错误 2 fatal error LNK1120: 1 个无法解析的外部命令 F:\Program Files\source\CP16\Debug\CP16.exe CP16