template class with nested iterator
Nov 19, 2010 at 8:57pm UTC
Hi everyone, I've been fighting for hours on a problem and can't figure out the solution. Maybe some of you ran into something like this already and can help me understand the compiler's complaint? Thanks in advance.
I'm creating a BinaryTree class with templates and an embedded iterator. Here's the relevant lines of what I'm doing:
//in BinTree.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
template <class T>
class BinTree{
struct Node{
...
} *rootNode;
...
public :
BinTree(const T&);
~BinTree();
class iterator;
friend class iterator;
class iterator{
BinTree::Node& node;
public :
iterator(const Node&);
...
};
iterator root();
...
};
//in BinTree.cpp:
1 2 3 4 5 6 7 8
#include "BinTree.h"
...
template <class T>
BinTree<T>::iterator BinTree<T>::root(){
return iterator(*rootNode);
}
...
the compiler complains about the function definition, saying:
error: expected constructor, destructor, or type conversion before 'BinTree'
Nov 19, 2010 at 10:31pm UTC
You need to make use of the
typename keyword
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
template <class T>
class BinTree{
struct Node{
...
} *rootNode;
...
public :
BinTree(const T&);
~BinTree();
class iterator;
friend class iterator;
class iterator{
typename BinTree::Node& node; //Here *****************
public :
iterator(const Node&);
...
};
iterator root();
...
};
1 2 3 4 5 6 7 8
#include "BinTree.h"
...
template <class T>
typename BinTree<T>::iterator BinTree<T>::root(){ //and here *********
return iterator(*rootNode);
}
...
Nov 20, 2010 at 9:58am UTC
thanks a lot guestgulkan! that was it.
Topic archived. No new replies allowed.