Results 1 to 12 of 12

Thread: Qt and global variables

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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: Qt and global variables

    Sorry,

    maybe I am too stupid, but your code doesn't work for me. I take your code and just changed the name of the class:
    Qt Code:
    1. #ifndef CONFIGURATION_H
    2. #define CONFIGURATION_H
    3.  
    4. #include <QtCore>
    5. #include <QtSql>
    6.  
    7. class configuration
    8. {
    9. public:
    10. static configuration* instance()
    11. {
    12. static QMutex mutex;
    13. if ( !m_instance )
    14. {
    15. mutex.lock();
    16. if ( !m_instance )
    17. m_instance = new configuration;
    18. mutex.unlock();
    19. }
    20. return m_instance;
    21. }
    22.  
    23. private:
    24. configuration(){};
    25. configuration( const configuration& _instance ){};
    26. static configuration* m_instance;
    27.  
    28. // [...] some other functions
    29.  
    30. };
    31.  
    32. configuration* configuration::m_instance = NULL;
    33.  
    34. #endif
    To copy to clipboard, switch view to plain text mode 

    When compiling I receive the error (which I don't understand == don't know what to do/where the error could be):
    .comp/sw_normal.o: In function `operator delete(void*, void*)':
    /usr/include/qt4/QtCore/qatomic_i386.h:62: multiple definition of `configuration::m_instance'
    .comp/qvortaro.o:/home/lykurg/Programmierung/qvortaro/src/qvortaro.cpp:194: first defined here
    collect2: ld returned 1 exit status


    And by the way must it in your example in the wiki not be
    Qt Code:
    1. Singleton* Singleton::m_Instance = 0;
    To copy to clipboard, switch view to plain text mode 
    instead of
    Qt Code:
    1. Singleton* m_Instance = 0;
    To copy to clipboard, switch view to plain text mode 



    Thanks,
    Lykurg

    P.s: if I comment the function (on bottom of the file) in qvortaro.cpp:194 out, then this error pointed to now last function on the file.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt and global variables

    Quote Originally Posted by Lykurg View Post
    When compiling I receive the error (which I don't understand == don't know what to do/where the error could be):
    .comp/sw_normal.o: In function `operator delete(void*, void*)':
    /usr/include/qt4/QtCore/qatomic_i386.h:62: multiple definition of `configuration::m_instance'
    .comp/qvortaro.o:/home/lykurg/Programmierung/qvortaro/src/qvortaro.cpp:194: first defined here
    collect2: ld returned 1 exit status
    You have placed the definition of static member variable in a header file, therefore every .cpp file that includes it has it's own copy of that variable. Better move that line to one of the .cpp files, say configuration.cpp.

  3. The following user says thank you to jacek for this useful post:

    Lykurg (1st February 2007)

  4. #3
    Join Date
    May 2006
    Location
    Germany
    Posts
    108
    Thanks
    2
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt and global variables

    Quote Originally Posted by Lykurg View Post
    And by the way must it in your example in the wiki not be
    Qt Code:
    1. Singleton* Singleton::m_Instance = 0;
    To copy to clipboard, switch view to plain text mode 
    instead of
    Qt Code:
    1. Singleton* m_Instance = 0;
    To copy to clipboard, switch view to plain text mode 
    Yes, of course. Thank you, Lykurg for spotting it, and thank you, jacek, for correcting it before I spotted this thread

Similar Threads

  1. Replies: 1
    Last Post: 22nd January 2007, 09:41
  2. Declarate global parameters
    By Dark_Tower in forum Qt Programming
    Replies: 7
    Last Post: 11th December 2006, 18:01
  3. Global variables
    By Mariane in forum Newbie
    Replies: 14
    Last Post: 10th October 2006, 17:23
  4. Creating a global array in my code???
    By therealjag in forum General Programming
    Replies: 5
    Last Post: 13th March 2006, 11:13
  5. declaration of global variables???
    By pranav_kavi in forum Newbie
    Replies: 6
    Last Post: 31st January 2006, 19:56

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
  •  
Qt is a trademark of The Qt Company.