C
Christopher
I am trying to implement a generic tree class, as I had mentioned in a
previous post. I want it to be STL like, so I went and researched how
to implement an iterator and copied an example from a page online.
template <class T>
class GenericTree
{
private:
class Node
{
public:
Node(const T & data) {}
// other methods
};
Node * m_root;
public:
// Other methods
class Iterator : std::iterator<std::foward_iterator_tag, T>
{
public:
Iterator(Node * node)
:
m_node(node)
{}
// Other methods
T * operator -> ()
{
return (&*(GenericTree<T>::Iterator)*this);
}
private:
Node * m_node;
};
Iterator begin()
{
return Iterator(m_root);
}
// Other methods
};
When I use it with the following code:
GenericTree<int> tree;
GenericTree<int> Iterator it = tree.begin();
it->() = new int(0);
I get an error:
"expected unqualified-id before '(' token
In member function 'T* GenericTree<T>::Iterator:perator->() [with T
= int]'
Instantiated from here
dependant-name ' GenericTree<T>::Iterator' is a parsed non-type, but
instantiation yields a type
note: say 'typename GenericTree<T>::Iterator' is a type is meant
I assume I need to use the typename keyword somewhere, but am unclear
where. Is my syntax incorrect? Can someone give a correct syntax with
nested template classes?
previous post. I want it to be STL like, so I went and researched how
to implement an iterator and copied an example from a page online.
template <class T>
class GenericTree
{
private:
class Node
{
public:
Node(const T & data) {}
// other methods
};
Node * m_root;
public:
// Other methods
class Iterator : std::iterator<std::foward_iterator_tag, T>
{
public:
Iterator(Node * node)
:
m_node(node)
{}
// Other methods
T * operator -> ()
{
return (&*(GenericTree<T>::Iterator)*this);
}
private:
Node * m_node;
};
Iterator begin()
{
return Iterator(m_root);
}
// Other methods
};
When I use it with the following code:
GenericTree<int> tree;
GenericTree<int> Iterator it = tree.begin();
it->() = new int(0);
I get an error:
"expected unqualified-id before '(' token
In member function 'T* GenericTree<T>::Iterator:perator->() [with T
= int]'
Instantiated from here
dependant-name ' GenericTree<T>::Iterator' is a parsed non-type, but
instantiation yields a type
note: say 'typename GenericTree<T>::Iterator' is a type is meant
I assume I need to use the typename keyword somewhere, but am unclear
where. Is my syntax incorrect? Can someone give a correct syntax with
nested template classes?