Static variables and include?

Hi…,

I have include/access problems with static variables in my project, attached an example.

When I create my static variable staticVar in testApp.h, staticVar is initalized correctly and delivers correct results in my h-file (Hero.h), but not in my cpp-file (Hero.cpp), in the cpp-file it seams not be initalized. What I am doing wrong?

testApp.h:

#pragma once

#include "ofMain.h"


static int staticVar;

#include "Hero.h"

class testApp : public ofBaseApp{

    public:
        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);

        Hero hero;
        
};

Hero.h:

#pragma once

class Hero {

public:

    Hero(){};

    void Draw1(){ ofDrawBitmapString( ofToString(staticVar), 100, 100); } // this works !!!
    void Draw2();

};

Hero.cpp:

#ifndef _TEST_APP
#define _TEST_APP
#include "testApp.h"
#endif

#include "Hero.h"

void Hero::Draw2(){
    ofDrawBitmapString( ofToString(staticVar), 200, 200); // this doesn't work !!!
}

testApp.cpp

#ifndef _TEST_APP
#define _TEST_APP
#include "testApp.h"
#endif

//--------------------------------------------------------------
void testApp::setup(){
    staticVar = 999;
}

//--------------------------------------------------------------
void testApp::draw(){
    hero.Draw1();
    hero.Draw2();
}

//--------------------------------------------------------------
void testApp::keyPressed(int key){
    staticVar = 777;
}

//--------------------------------------------------------------
void testApp::keyReleased(int key){
    staticVar = 666;
}

Here all complete source files -> src.zip (1.6 KB)

Thanks

Mike

P.S.: This is tested with of 0.8.0 under MS Visual Studio Express 2012

You will need to use global variables (declared using the extern command) if you want to use variables across multiple cpp files. See eg. http://www.cplusplus.com/forum/general/21368/

Hi inoodle,

thank you for the “extern” tip, but that doesn’t help. The static variable is known in the cpp-file, but the assignment from this static variable to another variable doesn’t work, attached a debug picture, where the staticVar is set, but it doesn’t assign itself to another variable (called test).

Any further ideas?

Thanks

Mike

Hmmm, that is strange!
Working with external variables is a little tricky sometimes. You could try the approach given in the following link and use a separate header file for the external variable to make sure it is defined and declared properly: http://stackoverflow.com/questions/1433204/how-do-i-share-a-variable-between-source-files-in-c-with-extern-but-how
(and also make sure a value to the external variable is assigned before calling the hero.draw2(); function)

I really hope this helps - but I could be all wrong, I am not exactly a cpp expert (yet… :wink: )

Hi innodle,

I’ve searched in this forum and others a little bit more, but I don’t get it. In general I need a global (static) variable which I can access from all inner or outer h- and cpp-files. I will take a look at it tomorrow again.

Thanks

Michael

Hi all,

I am going nuts. I just want define some global variables, but I don’t get it. Is there an working example with globals. In the end I just need some array variables sprite[xxx], sound[xxx], etc, which I can play or draw from everywhere in my code. Or is this insane?

Thanks

Michael

I have done this recently using extern. The arrays are declared at the top of testApp.cpp like:

int sprite[numberOfSprites];
and then extern from any class.h file:
#define numberOfSprites 100
extern int sprite[numberOfSprites]; sorry if im misunderstanding your problem, but that worked for me

Hi vdu,

good to hear. For me that worked too, but then I started adding class.cpp files and from cpp-files these variables are not initialized, they are empty/0/NULL.

Do you have an idea how to use static variables also in cpp files?

Thanks

Michael

Hi

I always use static variables as class members. That way it is much easier to have control over them IMO. You can use a class just for this:

Globals.h

 class Globals{
 public:
     static int someInteger;
     static string someString;
 }

Globals.cpp

int Globals::someInteger = 10;
string Globals::someString = "test";

somewhere else

#include "Globals.h"

cout << Globals::someInteger << endl;
cout << Globals::someString << endl;

Hi underdoeg,

great, that worked here with int variables very fine. Now I have the next problem, I need no int variables, I need e.g. ofSoundPlayer objects, I can define static ofSoundPlayer objects in Globals.h, but I don’t get it to initialize it in Globals.cpp. Do you have another hint for me?

Thank you very much

Mike

Objects should work the same way. But depending on where and when you initialize them it is sometimes easier to store them as pointers. But for ofSoundPlayer both of those should work.

Globals.h

 class Globals{
 public:
     static ofSoundPlayer player;
     static ofSoundPlayer* playerRef;
 }

Globals.cpp

ofSoundPlayer Globals::player;
ofSoundPlayer* Globals::playerRef = NULL;

somewhere else

#include "Globals.h"

Globals::player.load("whatever");
Globals::playerRef = new ofSoundPLayer();
Globals::playerRef->load("whatever");
1 Like

Hi underdoeg,

that worked again very fine, thank you very much again. And sorry for my simple questions, I am a C++ self taught beginner.

Mike

Don’t worry. So am/was I as well :wink: