Reputation: 23
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
};
Upvotes: 0
Views: 1731
Reputation: 267
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.
Upvotes: 1