Results 1 to 9 of 9

Thread: global variable in QT: getting ISSUES

  1. #1
    Join Date
    Sep 2010
    Location
    Chennai
    Posts
    37
    Thanks
    8
    Qt products
    Qt4
    Platforms
    MacOS X

    Arrow global variable in QT: getting ISSUES

    hi,
    I am using following code for making global variable. But I am getting the error:
    :: error: duplicate symbol _m_pInstance in citybook.o and main.o
    :: error: collect2: ld returned 1 exit status

    My code : CityBookGlobalVariables.h
    Qt Code:
    1. #ifndef CITYBOOKGLOBALVARIABLES_H
    2. #define CITYBOOKGLOBALVARIABLES_H
    3. #include <QStringList>
    4. class CityBookGlobalVariables
    5. {
    6. private:
    7. CityBookGlobalVariables(){};
    8. CityBookGlobalVariables( const CityBookGlobalVariables& _instance ){};
    9. static CityBookGlobalVariables* m_pInstance;
    10.  
    11.  
    12. public:
    13.  
    14. QStringList m_cityList;
    15. static CityBookGlobalVariables* instance()
    16. {
    17. if ( !m_pInstance )
    18. m_pInstance = new CityBookGlobalVariables;
    19.  
    20. return m_pInstance;
    21. }
    22.  
    23. void setCityList(QStringList cityList)
    24. {
    25. m_cityList = cityList;
    26. }
    27.  
    28. QStringList getCityList()
    29. {
    30. return m_cityList;
    31. }
    32. };
    33.  
    34. CityBookGlobalVariables* m_pInstance = 0;
    35. #endif // CITYBOOKGLOBALVARIABLES_H
    To copy to clipboard, switch view to plain text mode 

    please help me. Thanks in advance
    Last edited by Girija; 19th September 2010 at 14:45.

  2. #2
    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: global variable in QT: getting ISSUES

    Put
    Qt Code:
    1. CityBookGlobalVariables* m_pInstance = 0;
    To copy to clipboard, switch view to plain text mode 
    outside your header file! And also please do note the [CODE][/CODE] tags.

    EDIT: also have a look at [WIKI]Singleton pattern[/WIKI].

  3. #3
    Join Date
    Sep 2010
    Location
    Chennai
    Posts
    37
    Thanks
    8
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: global variable in QT: getting ISSUES

    Thanks. could you please tell little bit more?

  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: global variable in QT: getting ISSUES

    Quote Originally Posted by Girija View Post
    could you please tell little bit more?
    Regarding what?

  5. #5
    Join Date
    Sep 2010
    Location
    Chennai
    Posts
    37
    Thanks
    8
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: global variable in QT: getting ISSUES

    Hi,, still I am getting the error and also getting lot of warnings. Am I on right way for using global varialbes?

  6. #6
    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: global variable in QT: getting ISSUES

    Clean your project, rerun qmake. This may help. Otherwise there is a perfect working solution in your wiki: [WIKI]Singleton Pattern[/WIKI]. If that then still don't work for you, please provide a minimal example reproducing your problem.

    And how do you use CityBookGlobalVariables in you other code?

  7. #7
    Join Date
    Sep 2010
    Location
    Chennai
    Posts
    37
    Thanks
    8
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: global variable in QT: getting ISSUES

    Hi,
    Actually, The problem is...
    I am trying to do store the some set of data in QStringList from one of my class. And get the value of QStringList from another class. So I want to make the QStringList as a global variable. But Still I am getting the same error.

    Please help me.

    Thanks in advance.

  8. #8
    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: global variable in QT: getting ISSUES

    Quote Originally Posted by Girija View Post
    Please help me.
    I can not help you, if you do not provide a minimal compilable program which reproduces your problem or simply show you other code.

  9. The following user says thank you to Lykurg for this useful post:

    Girija (20th September 2010)

  10. #9
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: global variable in QT: getting ISSUES

    You must DECLARE a global variable in the header file, but you must DEFINE it only once, in a compilable source (*.cpp) file. You're defining it in the header, and you're going to get multiple definition errors because there are several files that include that header.

    Stuff "C++ global variables" into Google if you don't understand the difference and where to place each in your code.

    Or, as suggested, look at the Singleton pattern, although this is often overkill when a simple global will do.

  11. The following user says thank you to SixDegrees for this useful post:

    Girija (20th September 2010)

Similar Threads

  1. Setting a global variable
    By Windsoarer in forum Qt Programming
    Replies: 3
    Last Post: 16th February 2010, 22:37
  2. how to declare a global variable right
    By Cruz in forum Newbie
    Replies: 13
    Last Post: 16th February 2010, 16:25
  3. linking a slider to a global variable
    By switch in forum Newbie
    Replies: 6
    Last Post: 5th August 2009, 08:02
  4. Link QVariant to global variable
    By jobrandt in forum Qt Programming
    Replies: 2
    Last Post: 8th May 2007, 10:18
  5. global variable
    By Shuchi Agrawal in forum General Programming
    Replies: 10
    Last Post: 15th February 2007, 04:19

Tags for this Thread

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.