读书人

为什么提示不能创建模板类了呢?该如何

发布时间: 2012-02-28 13:06:36 作者: rapoo

为什么提示不能创建模板类了呢?
#include<iostream>
using namespace std;
template<class T>
class Node
{
friend class NodeList;
private:
Node *prev;
Node *next;
T index;
public:
Node(void){prev=NULL;next=NULL;index=0;}
Node(T index){prev=NULL;next=NULL;this->index=index;}
};

template<class T>
class NodeList
{
private:
Node<T> *firstNode;
Node<T> *lastNode;
int length;
public:
NodeList(void)
{
firstNode=NULL;
lastNode=NULL;
length=0;
}
};
报告错误为:
E:\174\Test\Test6.cpp(46) : error C2989: 'NodeList' : template class has already been defined as a non-template class
E:\174\Test\Test6.cpp(6) : see declaration of 'NodeList'


[解决办法]
'NodeList' : template class has already been defined as a non-template class
=======================================================
NodeList已经被定义了,而且作为非模板类,看看在你是不是重复定义了
[解决办法]

探讨
对 解决了 因为我并没有声明为NodeList为模板类
正确的做法是:
template <class T>
class NodeList;

template <class T>
class Node
{
friend class NodeList <T>;
private:
Node *prev;
Node *next;
T index;
public:
Node(void){prev=NULL;next=NULL;index=0;}
Node(T index){prev=NULL;next=NULL;this->index=index;}
};

template <class T>
class NodeList
{
private:
Node <T…

[解决办法]
CSDN 高手多`` 以后在这里定居了

读书人网 >C++

热点推荐