Global Variables/functions

Hello again.

First of all I want to say sorry for opening so many threads but I have a huge load of questions right now.

So this again is a very basic one but google did not give me any answers. I have a simple OF application which consists of the standard testApp class and a custom class that I include into my testApp.

My question is: How can I access variables and functions I declared in testApp within myClass?

A really basic example for this would be a counterVariable in testApp that gets updated each frame.

Now I want to access this counter variable in myClass. I thought it would simply work something like this:

int theCounter = testApp->counterVariable;  

but unfortunately i get the errormessage that “testApp was not declared in this scope”.

I know I could point towards the counter but thats kind of like what I want to avoid since I would get a huge constructor for myClass if I had to link every variable manually.

You can create a testApp variable in your new class, then when you create an instance, you can pass a reference to testApp in the constructor for example:

in testApp:

NewClass * myNewClass=new NewClass(this);  

in your new class


testApp * myTestApp  


NewClass::NewClass(testApp app){  
    int theCounter = myTestApp->counterVariable;  

Indeed if you are calling method from testApp, you can just pass counter as a parameter

Another solution is to create that variable as global, outside testApp but it’s not a very good practice.

Also you can create a singleton to have access to that variables.

There’s a similar question to this in this post:

where it’s explained how to make a singleton.

okay, thanks. so if i get everything in those threads right it does not really make sense to go for global functions. its better(only way) to place them as a method of myClass i guess and try to access the things you need globaly differently.

I know this is an old question. But it’s relevant to me (and hopefully more people). I am trying this very thing. But i get compiling errors. CodeBlocks on windows 7.

C:\stuff\openframeworks\apps\myApps\test\src\NewClass.h|7|error: expected ')' before 'app'|
C:\stuff\openframeworks\apps\myApps\test\src\NewClass.h|10|error: 'ofApp' does not name a type|
C:\stuff\openframeworks\apps\myApps\test\src\ofApp.h|23|error: no matching function for call to 'NewClass::NewClass(ofApp* const)'|
C:\stuff\openframeworks\apps\myApps\test\src\ofApp.h|23|note: candidates are:|
C:\stuff\openframeworks\apps\myApps\test\src\NewClass.h|4|note: constexpr NewClass::NewClass()|
C:\stuff\openframeworks\apps\myApps\test\src\NewClass.h|4|note:   candidate expects 0 arguments, 1 provided|
C:\stuff\openframeworks\apps\myApps\test\src\NewClass.h|4|note: constexpr NewClass::NewClass(const NewClass&)|
C:\stuff\openframeworks\apps\myApps\test\src\NewClass.h|4|note:   no known conversion for argument 1 from 'ofApp* const' to 'const NewClass&'|
C:\stuff\openframeworks\apps\myApps\test\src\NewClass.h|4|note: constexpr NewClass::NewClass(NewClass&&)|
C:\stuff\openframeworks\apps\myApps\test\src\NewClass.h|4|note:   no known conversion for argument 1 from 'ofApp* const' to 'NewClass&&'|
||=== Build finished: 3 errors, 0 warnings (0 minutes, 1 seconds) ===|

Here are the files:


#pragma once
#include "ofMain.h"

class NewClass
        NewClass(ofApp app);
        ofApp* app;


#include "NewClass.h"

NewClass::NewClass(ofApp app)
    this->app = app;


#pragma once

#include "ofMain.h"
#include "NewClass.h"

class ofApp : public ofBaseApp{

		void setup();
		void update();
		void draw();

		void keyPressed(int key);
		void keyReleased(int key);
		void mouseMoved(int x, int y );
		void mouseDragged(int x, int y, int button);
		void mousePressed(int x, int y, int button);
		void mouseReleased(int x, int y, int button);
		void windowResized(int w, int h);
		void dragEvent(ofDragInfo dragInfo);
		void gotMessage(ofMessage msg);

		NewClass* test = new NewClass(this);

ofApp.cpp is untouched.

I have some ideas of what could be wrong.

  1. Shouldn’t I have the NewClass constructor take a pointer? Like so
    NewClass(ofApp* app);? I tried this without success.
  2. I don’t include “ofApp.h” in NewClass.h but if I do the compiler still says ofApp i undeclared and doesn’t name a type.

Any advice woud be much appreiciated.

I found a solution myself. I think the problem had to do with the includes creating a cyclic dependency. Could that be it?
Anyway I managed to compile by forward declaring ofApp in newClass.h and newClass in ofApp.h
Like this:


#pragma once

class ofApp;
#include "ofMain.h"
#include "ofApp.h"

class NewClass{...


#pragma once

class NewClass;
#include "ofMain.h"
#include "NewClass.h"

class ofApp : public ofBaseApp{...

This is something I still often have trouble with; needing to access functions in the main app from within a class.
I don’t seem to be able to implement the last solution by @dealerpriest, and I’m not sure what I’m doing wrong.

I’m using the class name MyClass instead of NewClass, and the instance I create in ofApp.h is called myClass_obj.
This is the error I get:
error: field ‘myClass_obj’ has incomplete type ‘MyClass’ MyClass myClass_obj;

Is there a new/better way to do this in 0.9.x?

I just remembered there is ofGetAppPtr(), which seems to do the trick for what I need. Made a minor change to the example code, as I wasn’t able to get it to work before because I didn’t add the #include "ofApp.h" line to my class.h file.