Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template subclass pointer problem

Tags:

c++

templates

g++

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

like image 553
m0tive Avatar asked May 04 '26 01:05

m0tive


1 Answers

You need

typename std::set< Leaf* >::iterator it = m_leaves.begin(); 
typename std::set< Leaf* >::iterator endit = m_leaves.end();

The type of std::set depends on another template argument and you have to tell the compiler that this is actually a type. gcc 4.5.0 produces a better error message.

The second error is similar:

template <class value_type>
typename QuadTree<value_type>::Leaf* QuadTree< value_type >::Insert ( const value_type & _x )
{
    // Insert stuff...
}

Leaf is a inner class to QuadTree. You need to name it as such and you need to specify the type of the QuadTree as the inner class depends on the template parameter.

Another thing: You have a typo in QuadTree in many places.

like image 54
pmr Avatar answered May 05 '26 16:05

pmr