Results 1 to 14 of 14

Thread: how to declare a global variable right

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default how to declare a global variable right

    Hi!

    Here is a real C++ noob question. I want to define a global variable that is changable with a slider and accessible to all objects in my project. I tried it like this:

    Qt Code:
    1. #ifndef CONSTANTS_H
    2. #define CONSTANTS_H
    3.  
    4. #define PI 3.14159265358979323846
    5. #define G 9.81
    6. #define FPS 90
    7.  
    8. extern double GAMMA;
    9. double GAMMA = 0.01;
    10.  
    11. #endif // CONSTANTS_H
    To copy to clipboard, switch view to plain text mode 

    Until now I only had the #defined globals and it worked great by including constants.h everywhere I needed them. Now I have a new parameter GAMMA, which is not only global, but also needs to be changed from time to time.

    Well, this doesn't work, otherwise I wouldn't be asking. I get a compile error:

    debug/passivewalker.o: In function `ZN5QLineC1Eiiii':
    c:/Qt/4.4.3/include/QtGui/../../src/gui/painting/qpainter.h.data+0x0): multiple definition of `GAMMA'
    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
    It's not that the name GAMMA is already taken. I tried renaming it to something really special and it didn't help. I would like to keep all globals, constant or not, in one header file. What am I doing wrong?

    Thanks
    Cruz

  2. #2
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to declare a global variable right

    Quote Originally Posted by Cruz View Post
    Qt Code:
    1. extern double GAMMA;
    2. double GAMMA = 0.01;
    To copy to clipboard, switch view to plain text mode 
    Well, this doesn't work,
    Remove
    Qt Code:
    1. extern double GAMMA;
    To copy to clipboard, switch view to plain text mode 
    from here..
    Use it everywhere else, where you want this variable.

  3. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to declare a global variable right

    No, that doesn't work either. Same error about multiple definition.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: how to declare a global variable right

    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.

  5. #5
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to declare a global variable right

    The problem must be something else. Even if I call my global variable:

    Qt Code:
    1. #ifndef CONSTANTS_H
    2. #define CONSTANTS_H
    3.  
    4. #define sgn(a)(((a) < 0) ? -1 : 1)
    5.  
    6. #define PI 3.14159265358979323846
    7. #define G 9.81
    8. #define TIME_STEP 0.00001
    9. #define SIZE_SCALE 100
    10. #define FPS 100
    11.  
    12. double RUMPELSTIELZCHEN123 = 0.01;
    13.  
    14. #endif // CONSTANTS_H
    To copy to clipboard, switch view to plain text mode 

    I get the error:

    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
    locale_facets.tcc does not declare a global variable called RUMPELSTIELZCHEN123.

  6. #6
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to declare a global variable right

    Quote Originally Posted by Cruz View Post
    No, that doesn't work either. Same error about multiple definition.
    Please define your global variable in main.cpp (the file contains main function).
    And try using the global variable after declaring it as extern.

  7. #7
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to declare a global variable right

    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?

  8. #8
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to declare a global variable right

    I created a new header called globs.h with nothing but GAMMA in it.

    Qt Code:
    1. #ifndef GLOBS_H_
    2. #define GLOBS_H_
    3.  
    4. double GAMMA = 0.01;
    5.  
    6. #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.

  9. #9
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to declare a global variable right

    If you want to accumulate all global variables, work like this.
    In header globs.h:
    Qt Code:
    1. extern int glob1;
    2. extern int glob2;
    To copy to clipboard, switch view to plain text mode 

    In source globs.cpp:
    Qt Code:
    1. int glob1 = 0;
    2. int glob2 = 0;
    To copy to clipboard, switch view to plain text mode 

  10. The following user says thank you to yogeshgokul for this useful post:

    Cruz (22nd July 2009)

  11. #10
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to declare a global variable right

    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.

  12. #11
    Join Date
    Jul 2009
    Location
    Italy, Pieve Ligure (GE)
    Posts
    55
    Thanks
    7
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to declare a global variable right

    Quote Originally Posted by Cruz View Post
    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

  13. The following user says thank you to miwarre for this useful post:

    Cruz (23rd July 2009)

  14. #12
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to declare a global variable right

    Quote Originally Posted by miwarre View Post
    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.
    Aaah yes, thank you!

  15. #13
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to declare a global variable right


  16. #14
    Join Date
    Feb 2010
    Posts
    12
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to declare a global variable right

    thanks, too...

Similar Threads

  1. nmake error during .pro compiling
    By mattia in forum Installation and Deployment
    Replies: 5
    Last Post: 18th June 2008, 10:15
  2. Link QVariant to global variable
    By jobrandt in forum Qt Programming
    Replies: 2
    Last Post: 8th May 2007, 10:18
  3. global variable
    By Shuchi Agrawal in forum General Programming
    Replies: 10
    Last Post: 15th February 2007, 04:19
  4. how to declare a friend variable ?
    By probine in forum General Programming
    Replies: 5
    Last Post: 27th March 2006, 15:00
  5. custom plugin designer property with out a variable?
    By high_flyer in forum Qt Programming
    Replies: 1
    Last Post: 15th March 2006, 19:11

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.