Remove from here..
Use it everywhere else, where you want this variable.
Remove from here..
Use it everywhere else, where you want this variable.
No, that doesn't work either. Same error about multiple definition.
Whatever the file locale_facets.tcc is, it already defined a variable called GAMMA. So you might want use a singleton pattern to store your values inside a class or use a namespace.
The problem must be something else. Even if I call my global variable:
Qt Code:
#ifndef CONSTANTS_H #define CONSTANTS_H #define sgn(a)(((a) < 0) ? -1 : 1) #define PI 3.14159265358979323846 #define G 9.81 #define TIME_STEP 0.00001 #define SIZE_SCALE 100 #define FPS 100 double RUMPELSTIELZCHEN123 = 0.01; #endif // CONSTANTS_HTo copy to clipboard, switch view to plain text mode
I get the error:
locale_facets.tcc does not declare a global variable called RUMPELSTIELZCHEN123.c:/Qt/4.4.3/include/QtGui/../../src/gui/painting/qpainter.h.data+0x0): multiple definition of `RUMPELSTIELZCHEN123'
debug/stickman.o:C:/Qt/2009.03/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/locale_facets.tcc:2497: first defined here
collect2: ld returned 1 exit status
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