C++类模板问题
#include "stdata.h"
#ifndef __LIST__H
#define __LIST__H
template<class T>
class mylist
{
public:
mylist();
~mylist();
void Initial(const T& stu);
bool IsEmpty();
void Insert(const T pos,const T x);
void Delete();
void Find(const T num);
void Add(const T st);
void print();
private:
typedef struct mynode
{
T stu;
mynode* next;
};
typedef mynode* Pnode;
typedef mynode Node;
Pnode head;
Pnode node;
};
#endif
#include "list.h"
template<class T>
mylist<T>::mylist()
{
node=NULL;
head=NULL;
}
template<class T>
void mylist<T>::Initial(const T& stu)
{
if(IsEmpty())
{
node=new Node;
head=new Node;
node->stu.st_ID=stu.st_ID;
node->stu.st_name=stu.st_name;
head->next=node;
node->next=NULL;
}
else
return;
}
template<class T>
bool mylist<T>::IsEmpty()
{
return head==NULL;
}
template<class T>
void mylist<T>::Insert(const T pos,const T x)
{
if (IsEmpty())
{
cout<<"this is an empty list, please initialize it first!";
return;
}
Pnode mid=head->next;
do
{
if(mid->stu.st_ID==pos.st_ID)
{
Pnode tmp=new Node;
tmp->stu.st_ID=x.st_ID;
tmp->stu.st_name=x.st_name;
tmp->next=mid->next;
mid->next=tmp;
}
mid=mid->next;
}while (mid!=NULL);
}
template<class T>
void mylist<T>::Delete()
{
if (!IsEmpty())
{
Pnode mid=head->next;
delete head;
head=NULL;
Pnode t;
while (mid->next!=NULL)
{
t=mid->next;
delete mid;
mid=t;
}
}
else
return;
}
template<class T>
mylist<T>::~mylist()
{
Delete();
}
template<class T>
void mylist<T>::Find(const T num)
{
if (IsEmpty())
{
cout<<"Empty!"<<"find error"<<endl;
return;
}
else
{
Pnode mid=head->next;
while (mid!=NULL)
{
if(atoi(mid->stu.st_ID.c_str())==atoi(num.st_ID.c_str()))
{
cout<<num.st_ID<<" "<<mid->stu.st_name<<endl;
break;
}
mid=mid->next;
}
if ((mid/*->next*/==NULL))
{
cout<<"There is no such student!"<<endl;
return;
}
}
}
template<class T>
void mylist<T>::Add(const T st)
{
if (IsEmpty())
{
cout<<"Empty!"<<"add error"<<endl;
return;
}
Pnode mid=head->next;
Pnode t=new Node;
while (mid->next!=NULL)
{
mid=mid->next;
}
t->stu.st_ID=st.st_ID;
t->stu.st_name=st.st_name;
mid->next=t;
t->next=NULL;
}
template<class T>
void mylist<T>::print()
{
if (head==NULL)
{
cout<<"This is an empty list!";
return;
}
Pnode m=head->next;
while (m!=NULL)
{
cout<<m->stu.st_ID<<" "<<m->stu.st_name<<endl;
m=m->next;
}
}
#include <string>
#include <iostream>
#ifndef __STDATA__H
#define __STDATA__H
using namespace std;
typedef string Name;
typedef string IDnum;
struct my_student
{
Name st_name;
IDnum st_ID;
};
typedef my_student Student;
#endif
在main里定义mylist<Student> mlist,后面用mlist操作时,VC2008提示public: __thiscall mylist<struct my_student>::~mylist<struct my_student>(void)" (??1?$mylist@Umy_student@@@@QAE@XZ),该符号在函数 _main 中被引用。
找了半天没发现是什么错误,请指教