单链表基于C++的模板实现问题,向模板高熟请教!
下面的程序为什么编译可以通过,链接却出错,
小弟不知道错在哪里,完整的程序如下“
///文件slist.h
template <class Type>
class List;
template <class Type>
class ListNode
{
friend class List<Type>;
public:
ListNode();
ListNode(const Type &item);
ListNode<Type> *NextNode()
{
return link;
}
void InsertAfter(ListNode<Type> *p);
ListNode<Type> *GetNode(const Type& item,ListNode<Type> *next);
ListNode<Type> *RemoveAfter();
private:
Type data;
ListNode<Type> *link;
};
template <class Type>
class List
{
public:
List(const Type &value)
{
last=first=new ListNode<Type>(value);
}
~List();
void MakeEmpty();
int Length() const;
ListNode<Type> *Find1(Type value);
ListNode<Type> *Find2(int i);
bool InsertBefore(Type value,int i);
bool InsertAfter(Type value,int i);
Type *Remove(int i);
Type *Get(int i);
private:
ListNode<Type> *first,*last;
};
// 文件slist.cpp
#include "slist.h"
template <class Type>
ListNode<Type>::ListNode():link(NULL)
{}
template <class Type>
ListNode<Type>::ListNode(const Type &item):data(item),link(NULL)
{}
template <class Type>
void ListNode<Type>::InsertAfter (ListNode<Type> *p)
{
p->link=link;
link=p;
}
template <class Type>
ListNode<Type> *ListNode<Type>::GetNode (const Type &item,ListNode<Type> *next=NULL)
{
ListNode<Type> *newNode=new ListNode<Type>(item);
newNode->link=next;
link=newNode;
return newNode;
}
template <class Type>
ListNode<Type> *ListNode<Type>::RemoveAfter ()
{
ListNode<Type> *tempptr=link;
if (tempptr==NULL)
return NULL;
link=tempptr->link;
return tempptr;
}
template <class Type>
List<Type>::~List()
{
MakeEmpty();
delete first;
}
template <class Type>
void List<Type>::MakeEmpty ()
{
ListNode<Type> *q;
while (first->link!=NULL)
{
q=first->link;
first->link=q->link;
delete q;
}
last=first;
}
template <class Type>
int List<Type>::Length () const
{
ListNode<Type> *p=first->link;
int count=0;
while (p!=NULL)
{
count++;
p=p->link;
}
return count;
}
template <class Type>
ListNode<Type> *List<Type>::Find1 (Type value)
{
ListNode<Type> *p=first->link;
while (p!=NULL && p->data!=value)
p=p->link;
return p;
}
template <class Type>
ListNode<Type> *List<Type>::Find2 (int i)
{
if (i<1)
return NULL;
ListNode<Type> *p=first->link ;
int j=1;
while (p!=NULL && j<i)
{
p=p->link ;
j++;
}
return p;
}
template <class Type>
bool List<Type>::InsertBefore (Type value,int i)
{
ListNode<Type> *p=Find(i-1);
if (p==NULL)
return false;
ListNode<Type> *newNode=GetNode(value,p->link);
if (p->link==NULL)
last=newNode;
p->link=newNode;
}
//文件testmain.cpp
#include "slist.h"
void main()
{
int i=100;
List<int> p(i);
}
连接错误如下:
Linking...
testmain.obj : error LNK2001: unresolved external symbol "public: __thiscall List<int>::~List<int>(void)" (??1?$List@H@@QAE@XZ)
testmain.obj : error LNK2001: unresolved external symbol "public: __thiscall ListNode<int>::ListNode<int>(int const &)" (??0?$ListNode@H@@QAE@ABH@Z)
Debug/slist.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.
slist.exe - 3 error(s), 0 warning(s)
请问错误在哪里,虽然有提示,但是小弟还是觉得没有错