模板类问题

zhouyp 2002-01-11 11:14:38
请看如下代码:

# include <iostream.h>

template <class T>
class Node
{
public:
Node (T& rt):data(rt),pNext(NULL),pPrev(NULL) {}
Node* pNext;
Node* pPrev;
T& data;
};

template <class T>
class Dlink
{
public:
Dlink();
void Add(T&);
void Remove(T&);
void PrintFlist();
void PrintLlist();
~Dlink();
protected:
Node<T> * pFirst;
Node<T> * pLast;
};

template <class T> Dlink<T>::Dlink()
{
pFirst=pLast=NULL;
}

template <class T>
void Dlink<T>::Add(T& t)
{
Node<T> * temp=new Node<T> (t);
temp->pNext=0;
pLast=temp;
if(pFirst==NULL)
pFirst=temp;
}

template <class T>
void Dlink<T>::Remove(T& t)
{
Node<T> *n=NULL;
for(Node<T> *pF=pFirst;pF;pF=pF->pNext)
if(pF->data==t)
{
n=pF;
break;
}
if(n==NULL)
return;
Node<T> *pPrevS=n->pPrev;
Node<T> *pNextS=n->pNext;

if(pPrevS)
pPrevS->pNext=pNextS;
else
pFirst=pNextS;
if(pNextS)
pNexts->pPrev=pPrevs;
else
pLast=pPrevS;
delete n;
}

template <class T>
Dlink<T>::~Dlink()
{
if(pFirst==NULL)
return ;
Node<T> *pN=pFirst;
Node<T> *pF;
do
{
pF=pN;
pN=pN->pNext;
delete pF;
}while(pN);
}

template <class T>
void Dlink<T>::PrintFlist()
{
for(Node <T> * pF=pFirst;pF;pF=pF->next)
cout<<pF->data<<" ";
cout<<endl;
}

template <class T>
void Dlink<T>::PrintLlist()
{
for(Node<T>*pF=pLast;pF;pF=pF->pPrev)
cout<<pF->data<<" ";
cout<<endl;
}


void main()
{
Dlink<double> dL1;
dL1.Add(34.5);
dL1.Add(57.8);
dL1.Add(22.6);
dL1.PrintFlist();
dL1.PrintLlist();

Dlink <int> dL2;
dL2.Add(23);
dL2.Add(58);
dL2.Add(69);
dL2.Add(77);
dL2.Add(91);
dL2.PrintFlist();
dL2.Remove(77);
dL2.PrintFlist();
}

错误结果如下:
--------------------Configuration: 双向链表模板类 - Win32 Debug--------------------
Compiling...
双向链表模板类.cpp
d:\study\c++小程序\双向链表模板类.cpp(104) : error C2664: 'Add' : cannot convert parameter 1 from 'const double' to 'double &'
A reference that is not to 'const' cannot be bound to a non-lvalue
d:\study\c++小程序\双向链表模板类.cpp(105) : error C2664: 'Add' : cannot convert parameter 1 from 'const double' to 'double &'
A reference that is not to 'const' cannot be bound to a non-lvalue
d:\study\c++小程序\双向链表模板类.cpp(106) : error C2664: 'Add' : cannot convert parameter 1 from 'const double' to 'double &'
A reference that is not to 'const' cannot be bound to a non-lvalue
d:\study\c++小程序\双向链表模板类.cpp(111) : error C2664: 'Add' : cannot convert parameter 1 from 'const int' to 'int &'
A reference that is not to 'const' cannot be bound to a non-lvalue
d:\study\c++小程序\双向链表模板类.cpp(112) : error C2664: 'Add' : cannot convert parameter 1 from 'const int' to 'int &'
A reference that is not to 'const' cannot be bound to a non-lvalue
d:\study\c++小程序\双向链表模板类.cpp(113) : error C2664: 'Add' : cannot convert parameter 1 from 'const int' to 'int &'
A reference that is not to 'const' cannot be bound to a non-lvalue
d:\study\c++小程序\双向链表模板类.cpp(114) : error C2664: 'Add' : cannot convert parameter 1 from 'const int' to 'int &'
A reference that is not to 'const' cannot be bound to a non-lvalue
d:\study\c++小程序\双向链表模板类.cpp(115) : error C2664: 'Add' : cannot convert parameter 1 from 'const int' to 'int &'
A reference that is not to 'const' cannot be bound to a non-lvalue
d:\study\c++小程序\双向链表模板类.cpp(117) : error C2664: 'Remove' : cannot convert parameter 1 from 'const int' to 'int &'
A reference that is not to 'const' cannot be bound to a non-lvalue
Error executing cl.exe.

双向链表模板类.obj - 9 error(s), 0 warning(s)


请高手指点!!!!!!!
...全文
102 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhouyp 2002-01-11
  • 打赏
  • 举报
回复
to phalanger(wolf):
  谢谢!问题解决,可是还不知为什么要这样改?
phalanger 2002-01-11
  • 打赏
  • 举报
回复
不好意思 打快了class Node里面的
T& data是不行的,应该是T data;才行。
phalanger 2002-01-11
  • 打赏
  • 举报
回复
还有Node没有改
template <class T>
class Node
{
public:
Node (const T& rt):data(rt),pNext(NULL),pPrev(NULL) {}
Node* pNext;
Node* pPrev;
T& data;
};

记得尽量使用const呀,看看effective C++吧
zhouyp 2002-01-11
  • 打赏
  • 举报
回复
按照上面几位朋友的指示,修改发现错误减少了,但又产生几下两个错误:
--------------------Configuration: 双向链表模板类 - Win32 Debug--------------------
Compiling...
双向链表模板类.cpp
D:\STUDY\C++小程序\双向链表模板类.cpp(36) : error C2664: '__thiscall Node<double>::Node<double>(double &)' : cannot convert parameter 1 from 'const double' to 'double &'
Conversion loses qualifiers
D:\STUDY\C++小程序\双向链表模板类.cpp(120) : while compiling class-template member function 'void __thiscall Dlink<double>::Add(const double &)'
D:\STUDY\C++小程序\双向链表模板类.cpp(36) : error C2664: '__thiscall Node<int>::Node<int>(int &)' : cannot convert parameter 1 from 'const int' to 'int &'
Conversion loses qualifiers
D:\STUDY\C++小程序\双向链表模板类.cpp(120) : while compiling class-template member function 'void __thiscall Dlink<int>::Add(const int &)'
Error executing cl.exe.

双向链表模板类.obj - 2 error(s), 0 warning(s)

wqf2 2002-01-11
  • 打赏
  • 举报
回复
这样声明
template <class T>
class Dlink
{
public:
Dlink();
void Add(const T&);
void Remove(const T&);
void PrintFlist();
void PrintLlist();
~Dlink();
protected:
Node<T> * pFirst;
Node<T> * pLast;
};
phalanger 2002-01-11
  • 打赏
  • 举报
回复
void Dlink<T>::Add(T& t)
应该写成:void Dlink<T>::Add(const T& t)
同样 void Dlink<T>::Remove(T& t)
应该写成:void Dlink<T>::Remove(const T& t)

cnss 2002-01-11
  • 打赏
  • 举报
回复
再弄个带参数初始化的构造函数

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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