0

For educational purposes I try implement something like std container. But I'm stuck on template function declaration. Need some help with syntax

Error: error C2039: 'Begin' : is not a member of 'SinglyLinkedList<T>'. 

Header:

template<class T>
class SinglyLinkedList
{
public:
    typedef Iterator<T> Iterator;

    SinglyLinkedList();
    SinglyLinkedList(const SinglyLinkedList & other);
    ~SinglyLinkedList();

    bool IsEmpty() { return !m_pHead }

    void PushFront(T data);
    T & Front();
    void PopFront();
    void Clean();

    Iterator<T> Begin(); //Error
//  Iterator<T> End();
//  Iterator<T> Delete(Iterator<T> it);

private:
    Node<T> * m_pHead;
};

cpp file:

template<class T>
Iterator<T> SinglyLinkedList<T>::Begin()
{

}

EDIT:

typedef Iterator<T> Iterator;

is just typedef so i can use SinglyLinkedList::Iterator for Iterator. I have Iterator class/ It looks like this:

template<class T>
class Iterator
{
    friend SinglyLinkedList<T>;

public:
    Iterator() : m_pLinkedList(0), m_pNode(0) {}

    ~Iterator(){};

private:
    Iterator(SinglyLinkedList<T> * pLinkedList, Node<T> * pNode) : m_pLinkedList(pLinkedList), m_pNode(pNode) {}

    SinglyLinkedList<T> * GetListPtr() { return m_pLinkedList; }
    Node<T> * GetNodePtr() { return m_pNode; }

    void SetListPtr(SinglyLinkedList<T> * pList) { m_pLinkedList = pList; }
    void SetNodePtr(Node<T> * pNode) { m_pNode = pNode; }

    SinglyLinkedList<T> * m_pLinkedList;
    Node<T> * m_pNode;

public:
    //some overloaded operators

};
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Vivee
  • 23
  • 1
  • 5
  • Just an observation but you need to use `SinglyLinkedList::Iterator SinglyLinkedList::Begin() {}` instead of what you have – Captain Obvlious Jan 21 '14 at 19:33
  • 1
    Just an observation, [try putting that in the .h file](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). – WhozCraig Jan 21 '14 at 19:35
  • @CaptainObvlious Like types should be fine as long as they're properly accounted for. I.e. the out-of-class def *could* use `Iterator` and the in-class decl could use either `Iterator` or `::Iterator` [See it live](http://ideone.com/feyz99) (or I misunderstood your statement). Regardless, I would do it as you describe just for my own sanity. – WhozCraig Jan 21 '14 at 19:50
  • @WhozCraig Quite right. Pretend it's been edited into a suggestion ;) – Captain Obvlious Jan 21 '14 at 20:46

1 Answers1

1

The implementation of the templates members of a class, must be in the .h file, not in a separate .cpp file. Each translation unit needs to know the implementation, in this way is able to create the correct instantiation for each different templetized call.

Also, the following line will be a problem for sure

typedef Iterator<T> Iterator;

because you are using the same name of the templetized class for your typedef, and that will produce undefined behaviour.

hidrargyro
  • 257
  • 1
  • 7
  • Transfer to h file and comment typedef helps. But how they implement something like list::Iterator? – Vivee Jan 21 '14 at 20:20
  • the problem with your typedef is not the name, you can use Iterator, but in that case call your Iterator class with a different name, something like 'Iterator_' then you will have: typedef Iterator_ Iterator; and there is no problem for the compiler – hidrargyro Jan 21 '14 at 20:33