Francesco Rizzi
Francesco Rizzi

Reputation: 631

C++ - Implement template methods of inner template class in a separate file

I'm implementing this class:

#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

I want to implement ListNode class in ListNodeCode.h, but i get this error:

[Error] specializing member 'List::ListNode::ListNode' requires 'template<>' syntax

And this is the only method, at the moment, inside ListNodeCode.h:

#ifndef LISTNODECODE_H
#define LISTNODECODE_H
template <typename NodeType> List<NodeType>::ListNode<NodeType>::ListNode(const NodeType& value,const ListNode<NodeType>*const pElement, const ListNode<NodeType>*const nElement){

this->value=new NodeType();
*(this->value)=value;

this->pElement=pElement;
this->nElement=nElement;

cout << "Node created, (Value: " << (*this->value) << ", previous: " << pElement << ", next: " << nElement;
}

#endif

How can I correctly implement it?

Upvotes: 3

Views: 1220

Answers (2)

songyuanyao
songyuanyao

Reputation: 172954

Note that ListNode is a member template; and there're two separate template parameters, one (i.e. ListType) for the enclosing template List, one (i.e. NodeType) for the member template ListNode, so the definition should be:

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) {
    // ...
}

Upvotes: 5

Bo Persson
Bo Persson

Reputation: 92301

It is a nested template, so you have to use both levels in the definition as well

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

Upvotes: 1

Related Questions