CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2010
    Posts
    3

    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    May 2009
    Posts
    2,413

    Re: Object defined recursively?

    Quote Originally Posted by timboyk12 View Post
    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.

  4. #4
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Object defined recursively?

    Quote Originally Posted by timboyk12 View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured