C++ C++;-在单独的文件中实现内部模板类的模板方法
我正在实现这个类:C++ C++;-在单独的文件中实现内部模板类的模板方法,c++,class,templates,definition,C++,Class,Templates,Definition,我正在实现这个类: #ifndef LIST_H #define LIST_H template <typename ListType> class List{ public: enum ListPosition{LIST_START=-2,LIST_END=-1}; enum Order{ASCENDANT,DESCENDANT}; template <typename NodeType> class ListNode{
#ifndef LIST_H
#define LIST_H
template <typename ListType> class List{
public:
enum ListPosition{LIST_START=-2,LIST_END=-1};
enum Order{ASCENDANT,DESCENDANT};
template <typename NodeType> class ListNode{
public:
ListNode(const NodeType &value,const ListNode<NodeType>*const pElement, const ListNode<NodeType>*const nElement);
~ListNode();
ListNode<NodeType> *const previous() const;
ListNode<NodeType> *const next() const;
void setPrevious(const ListNode<NodeType> *const pElement);
void setNext(const ListNode<NodeType> *const nElement);
void setValue(const NodeType& value);
private:
ListNode<NodeType> *pElement;
ListNode<NodeType> *nElement;
NodeType *value;
};
List();
List(const List<ListType> &list);
~List();
bool contains(const ListType& value) const;
ListType& get(const int pos) const;
ListNode<ListType>& getNode(const int pos) const;
void add(const ListType& value);
void add(const int pos, const ListType& value);
void addAll(const int pos, const List<ListType>& list);
void set(const int pos, const ListType& value);
void remove(const int pos);
void remove(const ListType& value);
void order(Order order);
int size() const;
bool operator==(const List<ListType>& list) const;
void operator=(const List<ListType>& list);
operator const char *() const;
ListType& operator[](const int pos) const;
const ListNode<ListType>& operator[](const ListType& value) const;
protected:
ListNode<ListType> *firstNode;
ListNode<ListType> *lastNode;
int _size;
};
#include "ListCode.h"
#include "ListNodeCode.h"
#endif
如何正确实现它?请注意,
ListNode
是一个;还有两个单独的模板参数,一个(即列表类型
)用于封闭模板列表
,一个(即节点类型
)用于成员模板列表节点
,因此定义应为:
template <typename ListType> // for the enclosing class template
template <typename NodeType> // for the member template
List<ListType>::ListNode<NodeType>::ListNode(const NodeType& value,const ListNode<NodeType>*const pElement, const ListNode<NodeType>*const nElement) {
// ...
}
template//用于封闭类模板
模板//用于成员模板
列表::ListNode::ListNode(常量节点类型和值、常量ListNode*常量元素、常量ListNode*常量元素){
// ...
}
它是一个嵌套模板,因此您必须在定义中同时使用这两个级别
template <typename ListType>
template <typename NodeType>
List<ListType>::ListNode<NodeType>::ListNode(const NodeType& value,const ListNode<NodeType>*const pElement, const ListNode<NodeType>*const nElement)
// ^---- Note that it is ListType here
模板
模板
列表::ListNode::ListNode(常量节点类型和值、常量ListNode*常量元素、常量ListNode*常量元素)
//^----注意这里是ListType
谢谢,成功了!但是,有没有一种方法可以使相同的模板参数同时适用于这两个类?如果是,它将是有用的,因为我不必使用“NodeType”模板parameter@FrancescoRizzi您的意思是ListNode
将始终使用与List
相同的模板参数?那么为什么不把它变成一个非模板类呢?value和其他属性的类型是否正确?(ListType而不是NodeType)已解决,现在一切正常。。非常感谢你的帮助!
template <typename ListType>
template <typename NodeType>
List<ListType>::ListNode<NodeType>::ListNode(const NodeType& value,const ListNode<NodeType>*const pElement, const ListNode<NodeType>*const nElement)
// ^---- Note that it is ListType here