Yes that works. But this can't be the final solution. I have constants in two places and everywhere I use it I have to specificly declare it as extern. Why doesn't it work in the header file?
I created a new header called globs.h with nothing but GAMMA in it.
Qt Code:
#ifndef GLOBS_H_ #define GLOBS_H_ double GAMMA = 0.01; #endif /* GLOBS_H_ */To copy to clipboard, switch view to plain text mode
If I use the header only in one place, everything works fine. I use it a second time and there you go, multiple definition of GAMMA. It's as if the #ifndef GLOBS_H protection wouldn't be working.
If you want to accumulate all global variables, work like this.
In header globs.h:
Qt Code:
extern int glob1; extern int glob2;To copy to clipboard, switch view to plain text mode
In source globs.cpp:
Qt Code:
int glob1 = 0; int glob2 = 0;To copy to clipboard, switch view to plain text mode
Cruz (22nd July 2009)
Ok I did that and it works. This is a good solution, but I still don't understand why it didn't work in the first place.
It didn't work because you also put the definition of the variable (double GAMMA = 0.01;) in the .h file; this way, if you #include this .h file in several .c/.cpp files, several GAMMA variables get defined, one for each .c/.cpp file (whence the multiple definitions error).
The final solution works because in the .h file there is only the declaration of the variable (extern double GAMMA;) and this can be repeated any number of times as long as it is always the same; the definition, on the other hand, is put in just one of the .c/.cpp files and then it is unique (=> no error).
Note that the #ifndef GLOBS_H directive does not work in the way you seem to imply: it does not work around including the same .h in multiple .cpp's; it only works around multiple inclusions of the same .h in a single .cpp.
For instance: include1.h includes include2.h and test.cpp includes both include1.h and include2.h;
result: include2.h is included twice and #ifndef blocks the second inclusion.
Does this make things a bit more clear?
Ciao!
Miwarre
Cruz (23rd July 2009)
Have look at this link:
http://www.gamedev.net/reference/art...rticle1798.asp
thanks, too...
Bookmarks