Iterator Implementation Compile error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pnayak
    New Member
    • Apr 2008
    • 9

    Iterator Implementation Compile error

    Hi,

    I am trying to implement an iterator to a List class. The code is included below. I get the following compiler error. At the point where I use a class that is defined inside a template (nList<T>::Link ). The Link class is defined inside of a template. (I assume that is possible?). Any one know what I doing wrong?

    Many Thanks,
    Purush

    Error from Compiler (gcc 3.4.6)
    nList.cc:52: error: expected `;' before "current_"
    *** Error code 1

    Stop in /usr/home/annu/work/c++/Containers/dlists.



    Code:
    #include <iostream>
    
    template<class T> class Iterator
    {
    	public:
    		virtual T* first() = 0;
    		virtual T* next() = 0;
    		virtual T* previous() = 0;
    };
    
    template<class T> class List_Iterator;
    
    template<class T> class nList
    {
    	public:
    		nList(): head_(0), tail_(0), size_(0) {}
    		void push_front(const T& r);
    		void push_back(const T& r);
    		void insert(int position, const T r);
    		int size() const;
    		void print() const;
    		class Bad_Range{};
    		//friend class List_Iterator<T>;
    		
    		class Link
    		{
    			public:
    			Link* next;
    			Link* previous;
    			T val;
    			Link(T v):next(0), previous(0),val(v) {}
    			Link():next(0), previous(0) {}
    		};
    
    	
    	private:
    		Link* head_;
    		Link* tail_;
    		int size_;
    };
    
    
    template<class T> class List_Iterator : public Iterator<T>
    {
    	public:
    		T* first();
    		T* next();
    		T* previous();
    		List_Iterator(nList<T>& l): list_(l){};
    	private:
    		nList<T>& list_;
    		nList<T>::Link current_;
    };
    
    //template<class T> T* List_Iterator<T>::first()
    //{
    //	return &list_.front();
    //}
    //
    //template<class T> T* List_Iterator<T>::next()
    //{
    //	if (current_)
    //		current_ = current_->next;
    //
    //	return &current_->val;
    //}
    
    //template<class T> T* List_Iterator<T>::previous()
    //{
    //	if (current_)
    //		current_ = current_->previous;
    //
    //	return &current_->val;
    //}
    
    template<class T> int nList<T>::size() const
    {
    	return size_;
    }
    
    template<class T> void nList<T>::print() const
    {
    	Link* l;
    	
    	for (l = head_; l; l = l->next)
    		std::cout<<l->val<<'\n';
    
    }
    
    template<class T> void nList<T>::push_front(const T& r)
    {
    	Link* l = new Link(r);
    	
    	l->next = head_;
    	l->previous = 0;
    
    	if (head_)
    		head_->previous = l;
    
    	head_ = l;
    
    	if (!tail_)
    		tail_ = l;
    
    	size_++;
    }
    
    template<class T> void nList<T>::push_back(const T& r)
    {
    	Link* l = new Link(r);
    	
    	l->previous = tail_;
    	l->next = 0;
    
    	if (tail_)
    		tail_->next = l;
    
    	tail_ = l;
    
    	if (!head_)
    		head_ = l;
    
    	size_++;
    }
    
    template<class T> void nList<T>::insert(int position, const T r)
    {
    
    	if (position > size_)
    		throw Bad_Range();
    
    	Link* l = new Link(r);
    	
    	Link* iterator = head_;
    	for (int i = 0; i < position; i++)
    		iterator = iterator->next ;
    
    	if (iterator)
    	{
    		iterator->previous->next = l;
    		l->next = iterator;
    		iterator->previous = l;
    	}
    	else
    	{
    		push_back(r);
    	}
    	size_++;
    }
    
    int main()
    {
    	nList<int> il;
    }
    Last edited by pnayak; Apr 25 '08, 03:17 AM. Reason: more explanation
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    I havent gone through the code.
    But i tried compiling in my G++(cygwin) version 2.95.3-4 and it compiled successfully


    raghuram

    Comment

    • pnayak
      New Member
      • Apr 2008
      • 9

      #3
      I tried on another box with a 4.2 version of gcc and same problem. However the Error message in this case is,

      nList.cc:50: error: type 'nList<T>' is not derived from type 'List_Iterator< T>'
      nList.cc:50: error: expected ';' before 'current_'

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by pnayak
        nList<T>::Link current_;
        is a thing called a dependent type. Probably the class should look like:
        [code=cpp]
        template<class T> class List_Iterator : public Iterator<T>
        {
        public:
        T* first();
        T* next();
        T* previous();
        List_Iterator(n List<typename T>& l): list_(l){};
        private:
        nList& list_;
        nList::Link current_;
        };
        [/code]

        Visual Studio.NET 2008 gets the same error you get but that error goes away when the class is changed as above.

        Comment

        Working...