0

I am getting the following when trying to compile the code below:

preallocarray.h: In member function 'void PreallocArray<T>::push_back(const T&)':
preallocarray.h:82: error: expected `;' before 'itr'

I have a nested const_iterator class inside of my LinkedList class I created. It's been many years since I've done C++, so this is probably caused by something silly, but I've been hacking around and googling for an hour with no luck...

Here's my linked list class definition, decared in linklist.h:

template <typename T> 
class LinkedList 
{
    <snip...>
    public:
        class const_iterator
        {
        <snip...>
        };
    <snip...>
};

Then I have a second class, declared in preallocarray.h, as follows:

#include "linklist.h"
template <typename T>
class PreallocArray
{
    <snip...>
    public:
        void push_back( const T & newValue )
        {
            if (capacity == size)
            {
                allocateNode( capacity / 2 );
            }
            LinkedList<Node>::const_iterator itr; // error occurs here
            theList.end();
        }
    <snip...>

    private:
        LinkedList<Node> theList; // the linked list of nodes
    <snip...>
};
squawknull
  • 5,131
  • 2
  • 17
  • 27
  • That is very strange. I see you left out a bit of code, could you edit in the rest? The code snippets that you showed here look completely correct. – TravisG Jun 19 '11 at 15:37
  • Sure. I was just afraid that would obfuscate the problem. Note, when I comment out the line indicating where the error occurs, the error does go away... Also, I have tested the const_iterator and LinkedList classes separately. – squawknull Jun 19 '11 at 15:40
  • When the error occurs here `LinkedList::const_iterator itr`, what is `Node`? – Bo Persson Jun 19 '11 at 15:41

1 Answers1

2
LinkedList<Node>::const_iterator itr; // error occurs here

Based on the fact the enclosing class is actually a class template, I suspect that you intended to write T instead of Node. What is Node by the way? And where is it defined?

If you really wanted to write T, then you've to write it as:

typename LinkedList<T>::const_iterator itr; 

Don't forget to write typename as I've written above. If you've to write typename even if the template parameter is Node but its definition depends on T in some way:

typename LinkedList<Node>::const_iterator itr;//if Node depends on T

To know why typename is needed, see this answer by @Johannes Schaub:

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 2
    Even if he did mean to write `Node`, if the definition of `Node` depends on `T` your answer is still correct. – Sven Jun 19 '11 at 15:43
  • 1
    The missing `typename` was the problem. I changed it to `typename LinkedList::const_iterator itr;`, and it works! Thanks! Note, Node was declared further up as a struct in the PreallocArray. – squawknull Jun 19 '11 at 15:43
  • 1
    @squawknull: Well my guess is right. Now accept the answer :P – Nawaz Jun 19 '11 at 15:44
  • 1
    Just to explain why this is: because `Node` is dependant on the template parameter (in this case because it's nested in the template class), the compiler does not know what specialization `LinkedList` will expand to until the `PreallocArray` template itself is instantiated, so it doesn't know for sure whether `LinkedList::const_iterator` denotes a type (it could be a static member - or not exist at all - in some specialisations), so you have to explicitly inform the compiler that you want to treat it as a type name. – Sven Jun 19 '11 at 15:54
  • @Sven: Gave the link to the explanation by *@Johannes Schaub*. I think he has explained it beautifully. :-) – Nawaz Jun 19 '11 at 15:57