用模板实现链表的插入问题,提示三个错误! 请指教!!!!

wlfcamal 2005-04-01 01:57:47
#include<iostream>
using namespace std;

template <class T>
class LList
{
protected:
struct Node
{
T Data; // error C2079: “LList<T>::Node::Data”使用未定义的 class“T”
Node * Next;
} ;

Node * first;
int NumberNodes;

public:
LList(){ NumberNodes=0; Node * first=NULL;}
~LList();
int Length();
void Insertfirst(T el);

};



LList <class T>::~LList()
{
if( first!=NULL)
{
delete first;
first=NULL;
}

NumberNodes=0;

}

int LList <class T>::Length()
{
return NumberNodes;
}

void LList <class T>::Insertfirst(T e)
{
Node* newnode=new Node;
newnode->Data=e; // error C2440: “=” : 无法从“T”转换为“int”

if(NumberNodes==0)
first->Data=e; // error C2440: “=” : 无法从“T”转换为“int”

else
newnode->Next=first->Next;
first->Next=newnode;

NumberNodes++;
}


int main()
{
LList<int> L;
L.Insertfirst(2);
L.Insertfirst(3);
L.Length();
getchar();
return 0;
}

我感觉很可能是类内嵌套了结构,所以结构了面的T编译器不能识别,请问怎么解决!!!!!!
...全文
52 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
szws 2005-04-01
  • 打赏
  • 举报
回复
template <class T>
class LList
{
protected:
struct Node
{
T Data; // error C2079: “LList<T>::Node::Data”使用未定义的 class“T”
Node * Next;
} ;

Node * first;
int NumberNodes;

public:
LList(){ NumberNodes=0; Node * first=NULL;}
~LList();
int Length();
void Insertfirst(T el);

};



template <class T>
LList <T>::~LList()
{
if( first!=NULL)
{
delete first;
first=NULL;
}

NumberNodes=0;

}

template <class T>
int LList <T>::Length()
{
return NumberNodes;
}

template <class T>
void LList <T>::Insertfirst(T e)
{
Node* newnode=new Node;

newnode->Data=e;
if(NumberNodes==0)
first->Data=e;
else
newnode->Next=first->Next;
first->Next=newnode;

NumberNodes++;
}
szws 2005-04-01
  • 打赏
  • 举报
回复
类成员函数实现的时候
也要加:
template <class T>

64,642

社区成员

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

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