Referencing next class in template class, gettng error "expected ';'"
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...>
};
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:
- Where and why do I have to put the "template" and "typename" keywords?
精彩评论