static variables -> symbol not found errors

Hi,

i’m trying to create a couple classes that would store some settings that i use through out my program.

i made these classes

  
  
class ParticleSettings{  
public:  
	static  float gravity;  
	static  int millisBeforeFalling;  
	static  int numParticles;  
	static  int numFallingParticles;  
	static   float particleRadius;  
};  
  
class PaddleSettings{  
public:  
	static int numGrabbedParticles;  
	static float speedToCollide;  
	  
	PaddleSettings();  
};  
  

my idea is that now i could change their value using ofxSimpleGuiToo sliders and could access its value anywhere by using

  
  
float myParticleRadius = ParticleSettings::particleRadius;  
  

i keep getting these errors:

“ParticleSettings::particleRadius”, referenced from:
__ZN16ParticleSettings14particleRadiusE$non_lazy_ptr in ColorParticle.o
symbol(s) not found
collect2: ld returned 1 exit status

if i declare the variables at global scope it works but i would much rather store them inside classes or structs to keep things organized.
plus i would like to know why this isn’t working.

any help is appreciated.

Rui

hey rui

when you declare a var as static it needs to be defined in a cpp like:

float ParticleSettings::gravity;

Hi Arturo,

thanks, it works now :slight_smile:
could you explain a little why i need to do that?

Rui

in the case of instance members, the creation of the variable is done each time you create an instance of that object, but in the case of static data members, they don’t belong to any instance so you need to create the actual variable: to allocate the memory for that var in some place, so you define it in the cpp. it’s called external linkage, vars outside classes with the extern keyword work just the same.

it has to do with .h files being included in several parts of your code, so if you define something there it will get defined several times. in the case of instance vars you’re not actually defining them, just declaring that a var with that name is going to be created for each instance.

btw. in the definition you can actually give the var a value, like:

float ParticleSettings::gravity = 0.9;

i get it now!

thanks a lot :slight_smile:

Rui