|
-
June 21st, 2010, 04:41 PM
#1
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!
-
June 21st, 2010, 04:50 PM
#2
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.
-
June 21st, 2010, 05:47 PM
#3
Re: Object defined recursively?
 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.
-
June 22nd, 2010, 08:14 AM
#4
Re: Object defined recursively?
 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.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|