用C++写的关于链表的基本操作,在编译通过的情况下,构造时出现了如下错误,何解?
Linking...
main.obj : error LNK2001: unresolved external symbol "public: void __thiscall Link<int>::OutLink(void)" (?OutLink@?$Link@H@@QAEXXZ)
main.obj : error LNK2001: unresolved external symbol "public: int __thiscall Link<int>::InsertLink(int const &)" (?InsertLink@?$Link@H@@QAEHABH@Z)
main.obj : error LNK2001: unresolved external symbol "public: __thiscall Link<int>::Link<int>(void)" (??0?$Link@H@@QAE@XZ)
Debug/LinkNode.exe : fatal error LNK1120: 3 unresolved externals
Error executing link.exe.
LinkNode.exe - 4 error(s), 0 warning(s)
以下是源程序:
////////////////////Link.h
#ifndef LINK_H
#define LINK_H
template<class T>
struct LinkNode
{
T data;
struct LinkNode *next;
};
template<class T>
class Link
{
private:
LinkNode<T> *head;
int LinkSize;
public:
Link();
void InitLink(); //初始化链表
void OutLink(); //输出链表
int SearchLink(const T&); //查找
int InsertLink(const T&); //插入
int DeleteLink(const T&); //删除一个结点
int Delete(); //删除整个链表
~Link(){};
};
#endif
////////////////////////Link.cpp
#include"Link.h"
#include<iostream.h>
template<class T>
Link<T>::Link()
{
LinkSize=0;
}
template<class T>
void Link<T>::InitLink()
{
LinkNode<T> *p;
p=new LinkNode<T>;
//p->data=0;
p->next=NULL;
head=p;
}
template<class T>
void Link<T>::OutLink()
{
LinkNode<T> *p;
p=head->next;
while(p!=NULL) {cout<<p->data<<" ";p=p->next;}
}
template<class T>
int Link<T>::SearchLink(const T &a)
{
LinkNode<T> *p;
int i=0;
p=head->next;
while(p!=NULL)
{
if(p->data==a) {cout<<"查找成功,第一次找到为第"<<i<<"个结点。"<<endl;return 1;}
else {p=p->next;i++;}
}
return 0;
}
template<class T>
int Link<T>::InsertLink(const T &a)
{
LinkNode<T> *p,*q;
q=head->next;
p=new LinkNode<T>;
p->data=a;
p->next=NULL;
while(q->next!=NULL) q=q->next;
q->next=p;
LinkSize++;
return 1;
}
template<class T>
int Link<T>::DeleteLink(const T &a)
{
LinkNode<T> *pre,*p,*q;
pre=head;
p=pre->next;
while(p!=NULL)
{
if(p->data==a)
{
q=p;
pre->next=p->next;
delete q;
p=pre->next;
cout<<"删除成功,首次找到的结点为第"<<LinkSize<<"个。"<<endl;
LinkSize--;
return 1;
}
else p=p->next;
}
return 0;
}
template<class T>
int Link<T>::Delete()
{
LinkNode<T> *p;
if(head->next==NULL) return 0;
while(head!=NULL)
{
p=head;
head=head->next;
delete p;
LinkSize--;
}
return 1;
}
/////////////////////////main.cpp
#include"Link.h"
int main()
{
Link<int> q;
q.InsertLink(8);
q.OutLink();
return 0;
}