Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;-在单独的文件中实现内部模板类的模板方法_C++_Class_Templates_Definition - Fatal编程技术网

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