模板类问题
请看如下代码:
# 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)
请高手指点!!!!!!!