#include problem!

Hello everyone!

I supposed this is more a c++ question but I can’t find a solution to this:

I just want to pass a pointer to my testApp to an object inside testApp, it looks like this:

  
  
#ifndef _TEST_APP  
#define _TEST_APP  
  
#include "ofMain.h"  
#include "loader.h"  
  
class testApp : public ofBaseApp{  
	public:  
        Loader loader;  
};  
  
#endif  
  

  
  
#ifndef LOADER_H  
#define LOADER_H  
  
#include "ofMain.h"  
#include "ofxThread.h"  
#include "testApp.h"  
  
class Loader : public ofxThread {  
    public:  
        void start(testApp* p){  
            parent      = p;  
        }  
        testApp* parent;  
};  
  
#endif  
  
  

  
  
#include "testApp.h"  
void testApp::setup(){  
    loader.start(this);  
}  
  

I guess it’s because testApp.h is including loader.h wish is also including testApp.h, etc, … but I thought that the #ifndef / #define was there for this purpose!

Is there any way to do this easily?

Thanks a lot for any help!

Simon.

well the way you want it to do right now simply wont work.- Also its not ment to be done like that in c++.- You should take a look on how to define global variables in c++ a few keywords that could help you there are “static” “extern” aswell as singleton.

What do you need the pointer to testApp for anyways?

EDIT:
I think what would work in your case though is if you take apointer to *BaseApp. That would allow you to have the reference to your running app, but its still no good coding.

Thanks Moka,
As fast as usual :slight_smile:

one way to avoid this recursive include problem is with “forward declaration”

http://www.adp-gmbh.ch/cpp/forward-decl.html

it would look like this:

  
  
  
#ifndef LOADER_H  
#define LOADER_H  
  
#include "ofMain.h"  
#include "ofxThread.h"  
  
class testApp; // this is a "FORWARD DECLARATION", we'll include in the .cpp instead  
  
class Loader : public ofxThread {  
    public:  
        void start(testApp* p){  
            parent      = p;  
        }  
        testApp* parent;  
};  
  
#endif  
  
in loader.cpp:  
  
#include "loader.h"  
#include "testApp.h"  // now, we can use testApp in this .cpp as needed since we know what it does.   
  
  

hope that helps!
zach

well what I tend to do is to make one class which contains all the objects as statics like that:

  
  
  
class myGlobals{  
private:  
static myLoader * loader;  
static myRenderer * renderer;  
  
public:  
static void init(){  
loader = new myLoader();  
renderer = new myRenderer();  
}  
  
static myLoader & getLoader(){ return *loader; }  
static myLoader & getRenderer(){ return *renderer; }  
  
};  
  
  
  

the static keyword is really importand because the class rather works like a namespace this way. You simply include the myGlobals.h wherever you need it and then at program startup call

  
  
myGlobals::init();  
  

this is just one of many ways to make things global.

edit:
oh yeah zach is right, I forgot about forward declarations since I dont like to use it :slight_smile:

Thank you guys for your answers!

@zach: If I understand it right… It will only works if you put the include in the cpp file right?
@moka: Actually I discovered singleton and static methods something like one month ago and it’s really nice to see how you do this kind of global things!

Thanks! :slight_smile:

@simongeilfus - yes, you’ve got to include “testApp.h” in the .cpp file, otherwise, the compiler doesn’t know what functions or variables a given class has. btw, we use this technique in the opencv addon so that ofxCvGrayscaleImage can include a ofxCvColorImage and vice versa. It’s a *very* useful technique to know about since you an often get into situations where two classes need to include each other.

take care,
zach

Thanks a lot zach!
It’s much more clear in my head now!

Take care and happy new year to you and the rest of the OF crew!