How to access a variable from ofApp.cpp in another class?

I’m having a difficult time finding a working solution for accessing a variable defined in ofApp.cpp and ofApp.h in another class. The variety of solutions i’ve research and tried produce a variety of compiler or linker errors.

Here are the techniques i’ve tried:

  • using the extern keyword to define the define the variable in ofApp.h, assign a value to it in ofApp.cpp and the try to use it in myClass.cpp (which has #include ofApp.h) but the name is not recognized. (like this)
  • creating a get method in ofApp.cpp, and trying to call that method from 'myClass.cpp. Only works if the get method isstatic, but then thestatic` method can not access variables defined in the offApp class

I’m able to use a header file with lots of constants, defined with #define but I need a way to access variables that change values across multiple files.

Any hints/pointers would be much appreciated!

I’m working with OF 0.98 and XCode 8.2.1.

I’m not sure if this is what you want but this way allows you to access ofApp’s member variables from a class it uses.

in ofApp.h,

#include "ofMain.h"
#include "myClass.h"

class ofApp : public ofBaseApp{
    
	public:

    
        myClass myClass;
        int var;
};

in ofApp.cpp,

//--------------------------------------------------------------
void ofApp::setup(){
    
    myClass.var = &var;
    myClass.setup();
    cout << "VALUE : " << var << endl;
}

in myClass.h,

#include "ofMain.h"

class myClass {
    
public:
    
    void setup();
    
    int *var = reinterpret_cast<int*>(ofGetAppPtr());
    
};

in myClass.cpp,

//--------------------------------------------------------------
void myClass::setup(){
 
    *var = 1234567;
}

If you run the code, It prints

VALUE : 1234567

Or you can just use static variable like this.

in ofApp.h,

myClass myClass;
static int var;

in ofApp.cpp,

int ofApp::var;

//--------------------------------------------------------------
void ofApp::setup(){
    
    myClass.setup();
    cout << "VALUE : " << var << endl;
}

in myClass.h,

class myClass {
    
public:
    
    void setup();
    
};

in myClass.cpp

#include "myClass.h"
#include "ofApp.h"

//--------------------------------------------------------------
void myClass::setup(){
 
    ofApp::var = 1234567;
}

If you run the code, It prints

VALUE : 1234567

And lastly, you can use global variable using ‘extern’.

in ofApp.h,

#include "ofMain.h"
#include "myClass.h"

extern int var;

class ofApp : public ofBaseApp{
    
	public:
    
        myClass myClass;
};

in ofApp.cpp,

int var;

//--------------------------------------------------------------
void ofApp::setup(){
    
    myClass.setup();
    cout << "VALUE : " << var << endl;
}

in myClass.h,

#include "ofMain.h"

class myClass {
    
public:
    
    void setup();
    
};

in myClass.cpp,

#include "myClass.h"
#include "ofApp.h"

//--------------------------------------------------------------
void myClass::setup(){
 
    var = 1234567;
}

If you run the code, It prints

VALUE : 1234567

When I try to use extern I get a linker error when trying to build.

Here is what i have:

in offApp.h

#pragma once

#include "ofMain.h"
#include "myClass.h"

extern float camThresh;
extern float camSoftness;
extern float camInvert;

class ofApp : public ofBaseApp, public ofxMidiListener {

...

}

my other class is called vidLayer:

in vidLayer.h

#include "ofApp.h"
void myClass::setup() {
         thresh = camThresh;
         softness = camSoftness;
         invert = camInvert;
       
}

I get this linker error:

ld: warning: ignoring file /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/System/Library/Frameworks/QuickTime.framework/QuickTime.tbd, missing required architecture x86_64 in file /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/System/Library/Frameworks/QuickTime.framework/QuickTime.tbd
Undefined symbols for architecture x86_64:
  "_camThresh", referenced from:
      ofApp::setup() in ofApp.o
      ofApp::update() in ofApp.o
      ofApp::draw() in ofApp.o
      ofApp::newMidiMessage(ofxMidiMessage&) in ofApp.o
      vidLayer::setState(int) in vidLayer.o
  "_camSoftness", referenced from:
      ofApp::setup() in ofApp.o
      ofApp::update() in ofApp.o
      ofApp::draw() in ofApp.o
      ofApp::newMidiMessage(ofxMidiMessage&) in ofApp.o
      vidLayer::setState(int) in vidLayer.o
  "_camInvert", referenced from:
      ofApp::setup() in ofApp.o
      ofApp::update() in ofApp.o
      ofApp::draw() in ofApp.o
      ofApp::newMidiMessage(ofxMidiMessage&) in ofApp.o
      vidLayer::setState(int) in vidLayer.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

You should also define the extern variables in ofApp.cpp file

float camThresh;
float camSoftness;
float camInvert;

//--------------------------------------------------------------
void ofApp::setup(){
    
}

You can find more info in stackoverflow

When I try static, I get a linker error:

in ofApp.h:

#pragma once

#include "ofMain.h"

class ofApp : public ofBaseApp, public ofxMidiListener {

	public:
   //try static
        static float camThresh;
        static float camSoftness;
        static float camInvert;
 }

in my class, called vidLayer:

in vidLayer.cpp:

#include "ofApp.h"
#include "vidLayer.h"
#include "Consts.h"
void vidLayer::setup() {
               thresh = ofApp::camThresh;
                softness = ofApp::camSoftness;
                invert = ofApp::camInvert;
}

I get:

You should also define static member variables in ofApp.cpp file.

float ofApp::camThresh;
float ofApp::camSoftness;
float ofApp::camInvert;

//--------------------------------------------------------------
void ofApp::setup(){
    
}

great!

that worked.
Thank you @cuinjune

1 Like