0

我正在尝试使用模板类 LList 和嵌套迭代器和节点类来实现链接列表。这是代码:

template <typename T1>
class LList 
{
  public:
         class Iterator
         {
               public:
                      Iterator();
                      T1 get() const;
                      void next();
                      void previous();
                      bool equals(Iterator iter) const;
               private:
                       Node* position;
                       LList* container;
         };

         LList();
         ~LList();
         void pushBack(T1 data);
         Iterator begin();
         Iterator end();
         void insert (Iterator iter, T1 data);
         Iterator erase(Iterator Iter);
  private:
         class Node
         {
               public:
                      Node(T1 data);
               private:
                       T1 data;
                       Node* ptr_next;
                       Node* ptr_prev;
         };
         Node* ptr_first;
         Node* ptr_last;
};

template <typename T1> 
LList<T1>::Node::Node(T1 data)
{
     this->data = data;
     ptr_next = 0;
     ptr_prev =0;
}

template <typename T1> 
LList<T1>::Iterator::Iterator()
{
    position = 0;
    container = 0;
}

template <typename T1> 
T1 LList<T1>::Iterator::get() const
{
    return position->data;
}

template <typename T1> 
void LList<T1>::Iterator::next()
{
    if(position == container->ptr_last)
    {
                position = container->ptr_first;
    }
    else
    {
                position = position->ptr_next;
    }
}

template <typename T1> 
void LList<T1>::Iterator::previous()
{
 if(!position)
 {                  
                    position = container->ptr_last;
 }
 else
 {
     position = position->ptr_prev;
 }
}

template <typename T1> 
bool LList<T1>::Iterator::equals(Iterator iter) const
{
 return position == iter.position;
}

template <typename T1>      
LList<T1>::LList()
{
  ptr_first = 0;
  ptr_last = 0;
}

template <typename T1> 
LList<T1>::~LList()
{
  while (ptr_first)
  {
        Node* tmp = ptr_first;
        ptr_first = ptr_first->ptr_next;
        delete tmp;
        tmp = 0;
  }
}

template <typename T1> 
void LList<T1>::pushBack(T1 data)
{
 Node* new_node = new Node(data); 
 if(ptr_first==0)
 {
       ptr_first = new_node;
       ptr_last = new_node;
 }
 else
 {
     ptr_last->ptr_next = new_node;
     new_node->ptr_prev = ptr_last;
     ptr_last = new_node;
  }
 }          

 template <typename T1> 
 Iterator LList<T1>::begin()
 {
     Iterator iter;
     iter.positon = ptr_first;
     iter.container = this;
     return iter;
  }

template <typename T1> 
Iterator LList<T2>::end()
{
     Iterator iter;
     iter.position = ptr_last;
     iter.container = this;
     return iter;
 }

 template <typename T1> 
 void LList<T1>::insert(Iterator iter, T1 data)
 {
     if (iter.position == 0)
     {
         pushBack(data);
         return;
     }
     Node* before;
     Node* after;
     after = iter.position;
     before = iter.position->ptr_prev;
     Node* new_node = new Node(data);

     after->ptr_prev = new_node;
     if (before == 0) ptr_first = new_node;
     else before->ptr_next = new_node;

     new_node->ptr_prev = before;
     new_node->ptr_next = after;
  }                             

  template <typename T1> 
  Iterator LList<T1>::erase(Iterator iter)
  {
     Node* after = iter.position->ptr_next;
     Node* before = iter.position->ptr_prev;
     Node* remove = iter.position;
     if (remove == ptr_first) ptr_first = after;
     else before->ptr_next = after;
     if (remove == ptr_last) ptr_last = before;
     else after->ptr_prev = before;
     delete remove;
     remove = 0;
   }

我已经看到如何在没有嵌套类的情况下做到这一点,但我需要使用嵌套类来做到这一点。任何关于为什么它不能编译的帮助都会有所帮助:) 谢谢。

4

2 回答 2

1

有不同的错误:

第一个是你NodeIterator类中使用,但你需要Node在它之前声明:

template <typename T1>
class LList 
{     
private:
    class Node
    {
    public:
        Node(T1 data);
    private:
        T1 data;
        Node* ptr_next;
        Node* ptr_prev;
    };
    Node* ptr_first;
    Node* ptr_last;

public:
    LList();
    ~LList();
    void pushBack(T1 data);
class Iterator
    {
    public:
        Iterator();
        T1 get() const;
        void next();
        void previous();
        bool equals(Iterator iter) const;
    private:
        Node* position;
        LList* container;
    };
Iterator begin();
    Iterator end();
    void insert (Iterator iter, T1 data);
    Iterator erase(Iterator Iter);
};

这是第一件事。

第二个是Iterator嵌套类型,因此在返回此类型的 un 对象时需要更具体:

template <typename T1> 
typename LList<T1>::Iterator LList<T1>::begin()

最后一个错误是:

template <typename T1> 
Iterator LList<T2>::end()

这是正确的 lign :

template <typename T1> 
typename LList<T1>::Iterator LList<T1>::end()
// ^^^^^^^^^^^^^^^^^               ^^
于 2013-05-19T09:27:12.730 回答
1

嗯,Iterator是嵌套类的名称,所以当你在LList类模板的成员函数定义中使用它作为返回类型时,你必须完全限定它(并添加typename消歧符以告诉编译器后面的内容::应被解析为类型的名称)。

例如:

    template <typename T1>
    typename LList<T1>::Iterator LList<T1>::erase(Iterator iter)
//  ^^^^^^^^^^^^^^^^^^^^

此错误有几个实例,因此您必须修复所有这些实例。

您还指的是Node在其定义出现之前的类LList。因此,您应该对其进行前向声明

template <typename T1>
class LList
{
    class Node;
//  ^^^^^^^^^^^
//  Forward declaration for Node
public:
    // ...
    class Iterator
    {
        // ...
        Node* position; // <== Now this is OK because of the forward declaration
        // ...
     };
于 2013-05-19T09:15:07.170 回答