Results 1 to 8 of 8

Thread: C++ Static Functions

  1. #1
    Join Date
    Nov 2009
    Posts
    68
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded

    Default C++ Static Functions

    This may be the stupidest question ever asked on this forum but I have the following code.

    #Header File
    Qt Code:
    1. #ifndef GSETTINGS_H
    2. #define GSETTINGS_H
    3.  
    4. #include <QObject>
    5. #include <QSettings>
    6. #include <QString>
    7. #include <QPointer>
    8.  
    9. class gSettings : public QObject
    10. {
    11. private:
    12. public:
    13. gSettings();
    14. static bool OpenSettings(QString filename);
    15. static int i;
    16. };
    17.  
    18. #endif // GSETTINGS_H
    To copy to clipboard, switch view to plain text mode 

    # here is the implementation
    Qt Code:
    1. #include "gsettings.h"
    2. #include <QSettings>
    3.  
    4. gSettings::gSettings()
    5. {
    6. }
    7.  
    8. bool gSettings::OpenSettings(QString filename)
    9. {
    10. i = 12;
    11. return true;
    12. }
    To copy to clipboard, switch view to plain text mode 

    And I get the following error on the line "i=12"

    /home/weavert/Development/QT-Projects/Settings/Settings/gsettings.cpp:10: undefined reference to `gSettings::i'

    What am I doing wrong.?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: C++ Static Functions

    You need to initialize every static variable (gSettings::i in your case). Add this to your implementation code outside any method body (i.e. right beneath include statements):
    Qt Code:
    1. int gSettings::i = 0; // you can skip the "= 0" part if you like random values
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: C++ Static Functions

    Don't know. Broken compiler? It obviously knows the context as it's quoted it, but don't know why it says its undefined.

    The kind of code you've written will work fine on some compilers.

  4. #4
    Join Date
    Oct 2007
    Location
    Grenoble, France
    Posts
    80
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: C++ Static Functions

    Don't know. Broken compiler?
    It's not. Read what wysota wrote above.
    You have to run a level 3 diagnostic.

    Ashes to ashes, Qt to Qt ( wysota )

  5. #5
    Join Date
    Nov 2009
    Posts
    68
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded

    Default Re: C++ Static Functions

    Thanks so much!! That fixed it.

    That was not in my C++ book(s).

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: C++ Static Functions

    Quote Originally Posted by calhal View Post
    It's not. Read what wysota wrote above.
    Strange, I just copied and pasted the code and it works fine here. Maybe it's my compiler thats broken

  7. #7
    Join Date
    Oct 2007
    Location
    Grenoble, France
    Posts
    80
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: C++ Static Functions

    From Thinking in C++

    Because static data has a single piece of storage regardless of how many objects are created, that storage must be defined in a single place. The compiler will not allocate storage for you. The linker will report an error if a static data member is declared but not defined.

    The definition must occur outside the class (no inlining is allowed), and only one definition is allowed. Thus, it is common to put it in the implementation file for the class. The syntax sometimes gives people trouble, but it is actually quite logical. For example, if you create a static data member inside a class like this:
    Qt Code:
    1. class A {
    2. static int i;
    3. public:
    4. //...
    5. };
    To copy to clipboard, switch view to plain text mode 
    Then you must define storage for that static data member in the definition file like this:
    Qt Code:
    1. int A::i = 1;
    To copy to clipboard, switch view to plain text mode 
    You have to run a level 3 diagnostic.

    Ashes to ashes, Qt to Qt ( wysota )

  8. #8
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: C++ Static Functions

    Yes, It makes sense, I'm just wondering why my compiler doesn't require it.

    And now I know why - it was optimising it out. If I do some more work with it, such as assign it to a volatile or such like, then it quite happily throws up a linker error.

    You may have noticed I've never used a c++ static member variable. Static functions a plenty, but never variables.

Similar Threads

  1. Replies: 11
    Last Post: 13th July 2009, 17:05
  2. Replies: 16
    Last Post: 23rd May 2008, 11:12
  3. Replies: 2
    Last Post: 16th March 2007, 10:04
  4. Accessing to a static variable from the same class
    By xgoan in forum General Programming
    Replies: 6
    Last Post: 5th March 2007, 11:50
  5. Static functions and class members
    By Raistlin in forum General Programming
    Replies: 5
    Last Post: 22nd December 2006, 11:00

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.