Object defined recursively?
Hi everyone,
I'm working on an object (a Barnes-Hut tree) that contains a body, an octant, and 8 additional BHTrees inside of it. I originally wrote the code in java, where this worked quite simply. But now, I'm getting an error for each of the fields: error: field 'bTNW' has incomplete type
Here's the code:
#ifndef BHTREE_H
#define BHTREE_H
#include <iostream>
#include <math.h>
#include "Body.h"
#include "Oct.h"
using namespace std;
class BHTree {
Body body;
Oct oct;
BHTree bTNW;
BHTree bTNE;
BHTree bTSW;
BHTree bTSE;
BHTree bBNW;
BHTree bBNE;
BHTree bBSW;
BHTree bBSE;
public:
void set_values (Oct q);
// bool isExternal(BHTree t);
// void insert(Body b);
// void updateForce(Body b, double t);
};
void BHTree::set_values(Oct q){
oct=q;
}
#endif
I imagine it has something to do with C++'s forward declarations that don't really apply in java to functions and classes, but I'm not sure. Any help would be appreciated!
Re: Object defined recursively?
It is not possible to define the BHTree class with BHTree members. What you can do is to define BHTree* members.
Re: Object defined recursively?
Quote:
Originally Posted by
timboyk12
I originally wrote the code in java, where this worked quite simply.
It's because in Java you worked with pointers without much thinking about it (because every object is represented by a pointer (reference) to the object). This variable declaration in Java
BHTree x;
corresponds to this,
BHTree* x;
in C++.
In both cases you need to allocate objects dynamically using new. In C++ you must also delete the objects when you're finished with them. In Java you didn't need to because it has automatic garbage collection.
Re: Object defined recursively?
Quote:
Originally Posted by
timboyk12
I'm working on an object (a Barnes-Hut tree) that contains a body, an octant, and 8 additional BHTrees inside of it.
If the object contains a body, an octant and 8 objects, each of them containing a body, an octant and 8 objects, each of them containing a body, an octant and 8 objects, each of them ..... I don't think you have enough memory to create such an object. Actually I don't think anyone has.