Recursive Classes

Hi there,

I need to build class that has an array of the same class children, and this class is recursive. Basically as QuadTree algorithms or similar.

class Name{
       public: 

       Name();

       Name children [4]

};

This structure gives an error “Field has incomplete type ‘Name’”

You have to use a pointer for that to work.

class Name{
    public:

        Name();
        Name *children[4];
};

Edit: I should probably explain. If you instantiate an object of class “Name”, and upon instantiation it in turn instatiates a “Name” object, it will cause an infinite loop of memory allocation long past when the computer runs out of available memory. Creating a pointer means you’re telling the computer to recognize the variable, but not to give it a specific address in memory and construct a new “Name” object, thereby avoiding the problem.

Thanks, it is working.

There is only one issue:

class Name{

     public:

           Name(Node n_);
           Name *children[4];

}

struct Node{
 
   param1;
   param2; 
   param3;
   param4;
}

So, where I have to put struct Node and how I should call it?

That depends on what you’re trying to do with it. Can you elaborate a bit?

First of all, I need to pass certain set of parameters to each new Name class object, so there is no just Name(), but Name(params)

And to make it more compact instead of passing param1, param2, param3 …
want to do it in one struct object.

Name(param1, param2, param3 …) > Name(Node n_)

struct Node(
param1
param2
param3
param4)

I couldn’t get 1. there to put struct in .h or .cpp and how to make it working.

Basically, if I do like this:

.h:

struct Node{

         int x1, x2, y1, y2;
    };
    
    class Name{
        
        public:
       
        Name(Node n_);
        Name(Node n_, int level_);
    
        Name *children[4];
    
    };

There is no errors, but .cpp doesn’t recognise Node.

Hm… it seems now it’s working. Rrrrrrrrr!

And how I could add children?

Name(Node n){
  
    children[0] -> ofQuadTree(n);

}

I would recommend looking up a tutorial and trying to understand it before implementing the code. Here’s a good one I know of offhand (though it’s for octrees, not quadtrees, but the idea is very similar): http://www.brandonpelfrey.com/blog/coding-a-simple-octree/

He also includes a link to some sample code that you can read through to understand how it works. Do not copy the code directly, as it’s more meant for demonstrating the ideas behind an octree than as a robust implementation.

I haven’t followed this one, so I can’t vouch for it personally. Someone else recommended it though and it deals specifically with quadtrees: http://www.gamedev.net/page/resources/_/technical/graphics-programming-and-theory/quadtrees-r1303

To address your question specifically, the syntax for creating an object with a pointer uses the ‘new’ keyword:

Node tempNode;
children[0] = new Name(tempNode);

Although I don’t think that’s the best design. Read the links above to hammer out the design first. Hope that helps!

Cheers!

I am doing it as an exercise, so bad design is very useful in understanding how OF works.