【发布时间】:2014-02-11 14:12:19
【问题描述】:
出于教育目的,我尝试实现标准容器之类的东西。但我坚持模板函数声明。需要语法方面的帮助
Error: error C2039: 'Begin' : is not a member of 'SinglyLinkedList<T>'.
标题:
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 文件:
template<class T>
Iterator<T> SinglyLinkedList<T>::Begin()
{
}
编辑:
typedef Iterator<T> Iterator;
只是 typedef,所以我可以将 SinglyLinkedList::Iterator 用于 Iterator。我有迭代器类/它看起来像这样:
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
};
【问题讨论】:
-
只是一个观察,但你需要使用
SinglyLinkedList<T>::Iterator SinglyLinkedList<T>::Begin() {}而不是你所拥有的 -
@CaptainObvlious Like 类型应该没问题,只要它们被正确考虑。 IE。类外定义可以使用
Iterator<T>,类内decl可以使用Iterator或::Iterator<T>See it live(或者我误解了你的说法)。无论如何,我会按照你描述的那样做,只是为了我自己的理智。 -
@WhozCraig 完全正确。假装它已被编辑成建议;)