Troubles implementing iterator as nested class in linked list

Mar 30, 2013 at 9:42am
Hi all,

I have a template class, LinkedList, that I want to develop an iterator for. I have defined the iterator class in the private declarations area of the linked list.

But my compiler (in VS 2012) won't accept my declaration/definition of the LinkedList method getIterator(), that is supposed to return an Iterator object. It says definition does not match declaration (but to me it looks like it does). Furthermore, the compiler reacts in all sorts of ways on the line in my code that declares the getIterator() function, saying "missing ';' before identifier 'getIterator'", LinkedList<T>::Iterator<T>::{ctor} is not a memmber of a base class of LinkedList<T>" and "Iterator<int>: access declaration can only be applied to a base class member".

Here is my getIterator() declaration:

typename LinkedList<T>::Iterator<T>::Iterator getIterator();

And here is the definition:

1
2
3
4
template <typename T>
typename LinkedList<T>::Iterator<T>::Iterator LinkedList<T>::getIterator() {
	return new Iterator<T>(*this, *first);
}
Last edited on Mar 30, 2013 at 9:42am
Mar 30, 2013 at 9:59am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template < typename T > struct linked_list
{
    struct node { /* ... */ } ;
    node* first ;

    struct iterator { iterator( node* ) ; /* ... */ } ;

    iterator get_iterator() ;
};

template < typename T >
linked_list<T>::iterator::iterator( linked_list<T>::node* ) { /* ... */ }

template < typename T >
typename linked_list<T>::iterator linked_list<T>::get_iterator()
{ return iterator(first) ; }
Mar 30, 2013 at 11:24am
Thanks!

I changed the scope declarations and now I don't get errors.

But now I'm puzzled. How do I create Iterators from within my main?
Topic archived. No new replies allowed.