关于类模板的一个小问题

hbxtght 2011-11-03 06:16:52
代码如下:

#include <iostream>
#include <string>
using namespace std;

template<class T>
class Node
{
public:
T Record;
Node<T>* Next;
Node()
{
Record = T();
Next = NULL;
}
Node(T record)
{
Record = record;
Next = NULL;
}
friend ostream& operator<< (ostream& ost, const Node<T>& node)
{
ost<<node.Record;
return ost;
}
};

template<class T>
class LinkedList
{
protected:
Node<T>* _head;
Node<T>* _rear;
public:
LinkedList()
{
_head = NULL;
_rear = NULL;
}
bool IsEmpty()
{
if(_head == NULL)
{
return true;
}
else
{
return false;
}
}
void InsFirst(T record)
{
Node<T>* node = new Node<T>(record);
if(IsEmpty())
{
_head = node;
_rear = node;
}
else
{
node->Next = _head;
_head = node;
}
}
void InsLast(T record)
{
Node<T>* node = new Node<T>(record);
if(IsEmpty())
{
_head = node;
_rear = node;
}
else
{
_rear->Next = node;
_rear = node;
}
}
T DelFirst()
{
if(!IsEmpty())
{
Node<T>* node = _head;
T result = node->Record;
if(_head == _rear)
{
_head = NULL;
_rear = NULL;
delete node;
return result;
}
else
{
_head = _head->Next;
delete node;
return result;
}
}
else
{
T result;
return result;
}
}
~LinkedList()
{
while(!IsEmpty())
{
DelFirst();
}
}
LinkedList(LinkedList<T>& list)
{
if(list.IsEmpty())
{
_head = NULL;
_rear = NULL;
}
else
{
_head = NULL;
_rear = NULL;
Node<T>* node = list._head;
while(node != NULL)
{
InsLast(node->Record);
node = node->Next;
}
}
}
LinkedList<T>& operator= (const LinkedList<T>& list)
{
while (!IsEmpty())
{
DelFirst();
}
Node<T>* node = list._head;
while (node != NULL)
{
InsLast(node->Record);
node = node->Next;
}
}
friend ostream& operator<< (ostream& ost, const LinkedList<T>& list)
{
Node<T>* node = list._head;
while(node != NULL)
{
ost<<*node<<endl;
node = node->Next;
}
return ost;
}
};

template<class T>
class LinkedStack:protected LinkedList<T>
{
public:
LinkedStack():LinkedList<T>()
{
}
void Push(T record)
{
LinkedList<T>::InsFirst(record);
}
T Pop()
{
return LinkedList<T>::DelFirst();
}
bool IsEmpty()
{
return LinkedList<T>::IsEmpty();
}
friend ostream& operator<< (ostream& ost, const LinkedStack<T>& stack)
{
Node<T>* node = stack._head;
while(node != NULL)
{
ost<<*node<<endl;
node = node->Next;
}
return ost;
}
};

int main()
{

LinkedList<int> listInt1;
listInt1.InsFirst(1);
listInt1.InsFirst(2);
listInt1.InsFirst(3);
cout<<listInt1<<endl;

LinkedList<int> listInt2;
listInt2.InsFirst(10);
listInt2.InsFirst(20);
listInt2.InsFirst(30);
cout<<listInt2<<endl;

LinkedList<int> listInt3 = listInt2;
cout<<listInt3<<endl;

LinkedList< LinkedList<int> > finalList;
finalList.InsFirst(listInt1);
finalList.InsFirst(listInt2);
cout<<finalList<<endl;

return 0;
}

问题是:模板类LinkedList<T>中拷贝构造函数,重载赋值运算符函数,以及友元重载输出操作符函数的参数把LinkedList<T>改成LinkedList也不会出错,不是LinkedList<T>才代表一个完整的类吗?去掉<T>也行?
...全文
43 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq120848369 2011-11-03
  • 打赏
  • 举报
回复
[Quote=引用楼主 hbxtght 的回复:]
代码如下:


C/C++ code
#include <iostream>
#include <string>
using namespace std;

template<class T>
class Node
{
public:
T Record;
Node<T>* Next;
Node()
……
[/Quote]

在类内可以忽略,在类外定义函数实现的时候不能省略.
qunqun2012 2011-11-03
  • 打赏
  • 举报
回复
因为在类中, 你如果放在类外实现,你试试看

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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