In a moment of madness, I decided to write a quadtree C++ template class. I've run into some weird compiler error that I don't understand with regard to subclasses and pointers to templates. I've found some hacky work arounds, but I wondered if anyone could shed some light on why my code wouldn't compile...
I'm on Linux, building with scons, using g++
My code looks something like this, I have a template class to describe a tree and a subclass describing 'leaves':
template <class value_type>
class QuadTree
{
public:
class Leaf //-Subclass--------------------------
{
friend class QuadTree< value_type >;
protected:
value_type* m_data;
Leaf();
~Leaf();
}; //-end-subclass------------------------------
QuadTree();
~QuadTree();
Leaf * Insert ( const value_type & _x );
protected:
QuadTree( Quadtree< value_type >* _parent );
QuadTree< value_type >* m_parent;
QuadTree< value_type >* m_children[4];
std::set< Leaf* > m_leaves;
};
First pointer problem I get is in the QuadTree destructor:
template <class value_type>
QuadTree< value_type >::~QuadTree()
{
// ... Delete children ...
// I allocate each leaf, so I need to delete them
std::set< Leaf* >::iterator it = m_leaves.begin(); // <-- bad
std::set< Leaf* >::iterator endit = m_leaves.end(); // <-- bad
for(;it != endit; ++it)
delete *it;
}
When I compile I get this error: expected ';' before ‘it’
and expected ';' before ‘endit’
.
The other pointer error is in the Insert function definition:
template <class value_type>
Leaf * QuadTree< value_type >::Insert ( const value_type & _x ) // <-- bad
{
// Insert stuff...
}
I get the compile error: expected constructor, destructor, or type conversion before ‘*’ token
Anyone know why I'm getting these errors? I've got fixes for the problems, but I want to know why I can't do it this way.
Ps. I've edited the code to show it here, so it is possible I've missed something I thought utterly irrelevant.
Edit. Fixed Quadtree -> QuadTree typo