ofGetAppPtr() declaration error


Quite simple but I cant get it to work, so I declare 2 int variables on testApp.h,

class testApp : public ofBaseApp{  
	int screenWidth;  
	int screenHeight;  

and initialize them in testApp.cpp setup() method

void testApp::setup(){  
	ofBackground(0, 0, 0);  
	screenWidth  = ofGetWidth();  
	screenHeight = ofGetHeight();  

I create a class MyClass and I try to call the variables from testApp using

location.x = ofGetAppPtr()->screenWidth;  

Like the sample code from the docs http://www.openframeworks.cc/documentation?detail=ofAppRunner&adv=yes but I get the following error: class ofBaseApp’ has no member named 'screenWidth’

What am I missing?


Hi Rs,

ofGetAppPtr returns a pointer to an ofBaseApp. You’ll have to cast it to your inherited class:

  // quick'n'dirty:  
  location.x = dynamic_cast<testApp*>(ofGetAppPtr())->screenWidth;  
  // safer  
  testApp* app = dynamic_cast<testApp*>(ofGetAppPtr());  
  if (testApp) location.x = testApp->screenWidth;   

I haven’t tested this code, just typed into the box :slight_smile: And it seems the documentation is wrong in this regard.

Hope that helps,


Ok so this is a kind of c++ trick, but ofGetAppPtr returns a pointer to the baseApp, which your testApp extends. So if you just use what’s returned from ofGetAppPtr(), it doesn’t know what’s in testApp. you can access things ofBaseApp, like mouseX, mouseY, etc. ie:


here’s a short snippet of how to do what you’d like, then an explanation. It’s in a .cpp file, “particle.cpp”

#include "particle.h"  
#include "testApp.h"  
void particle::printCounter(){  
	cout << ((testApp*)ofGetAppPtr())->counter << endl;  

some points:

a) you need to include testApp.h in the .cpp for the file you’d like to access the main app from . this is because you are going to case as a (testApp*) so you need to know what’s in there. But, if you include it in the h file, you can get a recursive include problem, ie:

testApp.h says include particle.h
particle.h says include testApp.h

leads to an infinite loop.

b) you case the result of ofGetAppPtr() as a testApp*, so you can access stuff that’s inside of it, like you are trying to do.

hope that helps!

ps: I realize now that the documentation example is wrong in this regard - we’ll fix it up.

I will give it a try later on


@zach: Well no luck, if I try your suggestion of including testApp.h I get lots of errors

Any ideas?


what errors? can you post a zip?
did you include the testApp.h in the .cpp file?


I am using ofxiPhone and ofxBox2d if that give you a hint

Find the src attach



looks like you wrote it wrong:


should be :


can you give that a try?

  • zach

Hi zach

still about 3500 errors


sorry – I just tried it and there is a problem because testApp is compiled as .mm and not .cpp, so there’s some funkiness if you are including the .h file of a .mm in a .cpp. I can see a ton of errors relating to objective-c in a cpp world, etc. Converting blob.cpp to blob.mm didn’t help – so it will require some further research. For normal times when you are doing non iphone stuff, that trick should work fine.

for now, can you do:

a) in Blob.h, make a variable:

ofxBox2d * testAppsBox2d;  

b) in testApp.mm, do:

box2d.setGravity(0.0f, 0.0f);  
box2d.setIterations(2, 2);  
box2d.createBounds(0.0f, 0.0f, ofGetWidth(), ofGetHeight());  
blob.testAppsBox2d = &box2d;  

then you should be able to replace the code you’ve written, as:

void Blob::init() {  
	float radius = 10.0f;  
	// the joints  
	for(int i = 0; i < TOTAL_POINTS; i++) {  
		float x = (ofGetWidth() * 0.5f) + cos(i) * 50;   
		float y = 50 + sin(i) * 50;  
		ballJoints[i].setPhysics(3.0f, 0.53f, 0.1f);  
		ballJoints[i].setup(testAppsBox2d->getWorld(), x, y, 10);		  
	// connect all the ball joints  
	for(int i = 1; i < TOTAL_POINTS; i++) {  
		joints[i].addJoint(ballJoints[i].body, ballJoints[i - 1].body, 3.0, 0.5);  
		// set the first and last joint  
		if(i == TOTAL_POINTS - 1) {  
			joints[0].addJoint(ballJoints[TOTAL_POINTS - 1].body, ballJoints[0].body, 3.0, 0.5);  

also, make sure you are using the latest box2d:


which compiles fine for iphone.

hope that helps!

Hi zach,

No errors when I compile, but them I get the following “EXC_BAD_ACCESS”

and a red arrow in the line

ballJoints[i].setup(testAppsBox2d->getWorld(), x, y, 10);  

in the Blob init method

them I change

blob.testAppsBox2d = &box2d;  

I thought may be the error is because I am calling init() before declaring testAppsBox2d, but doing so I get the same bad access message in the previous line

ballJoints[i].setPhysics(3.0f, 0.53f, 0.1f);  

Any ideas?


Apparently this

ofxBox2dCircle ballJoints[5];  
ofxBox2dJoint joints[5];  

Does not initialize the objects properly and that is why I am getting the error