c++ - Template LinkedList class with nested class -


i'm trying implement linked list template class llist , nested iterator , node classes. here's code:

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;    } 

i've seen how without nested classes need nested class. on why doesn't compile :) thanks.

well, iterator name of nested class, when use in definition of member function of llist class template return type, have qualify (and add typename disambiguator tell compiler follows :: shall parsed name of type).

for instance:

    template <typename t1>     typename llist<t1>::iterator llist<t1>::erase(iterator iter) //  ^^^^^^^^^^^^^^^^^^^^ 

there several instances of error, have fix of them.

you referring class node before definition appears inside llist. therefore, should have forward declaration it:

template <typename t1> class llist {     class node; //  ^^^^^^^^^^^ //  forward declaration node public:     // ...     class iterator     {         // ...         node* position; // <== ok because of forward declaration         // ...      }; 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -