C++类模板问题

hh96581 2016-07-30 10:49:05
#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 中被引用。
找了半天没发现是什么错误,请指教
...全文
1234 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
shiter 2016-08-22
  • 打赏
  • 举报
回复
楼主找到解决方案了。就分享一下答案,赶紧结贴啦
kongl123 2016-08-12
  • 打赏
  • 举报
回复
楼上说的,模板和宏都是编译期的东西:编译你的实现文件时,编译器发现这些模板没人使用(编译期是按文件力度的,所以看不到你的main),没有使用的模板是实例化不了的
ID870177103 2016-07-31
  • 打赏
  • 举报
回复
typedef struct mynode { T stu; mynode* next; }; 除了这句,没什么问题的样子
paschen 2016-07-31
  • 打赏
  • 举报
回复
模板类不支持分离式编译,也就是说你得把类的声明和实现放在一个文件里 具体原因看;http://blog.csdn.net/nestler/article/details/38731021
ID870177103 2016-07-31
  • 打赏
  • 举报
回复
引用 2 楼 hh96581 的回复:
[quote=引用 1 楼 ID870177103 的回复:] typedef struct mynode { T stu; mynode* next; }; 除了这句,没什么问题的样子
是哪里有问题?我不把类设置成模板时,直接用Student来定义stu时候不出错[/quote] 你查查typedef的语法,另外你的问题原因应该是3楼说的
iyomumx 2016-07-31
  • 打赏
  • 举报
回复
你不会是把模板声明和实现分开了吧
hh96581 2016-07-31
  • 打赏
  • 举报
回复
引用 1 楼 ID870177103 的回复:
typedef struct mynode { T stu; mynode* next; }; 除了这句,没什么问题的样子
是哪里有问题?我不把类设置成模板时,直接用Student来定义stu时候不出错

5,530

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 模式及实现
社区管理员
  • 模式及实现社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧