Results 1 to 7 of 7

Thread: Accessing to a static variable from the same class

  1. #1
    Join Date
    Jul 2006
    Posts
    126
    Thanks
    17
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Accessing to a static variable from the same class

    Hi,

    I'm implementing a class to do operations with a SQLite database and I have this header file

    Qt Code:
    1. ...
    2. class ObjectDatabase{
    3. public:
    4. static bool openDatabase(QString);
    5. static bool createTables();
    6. static void closeDatabase();
    7. static bool vacuumDatabase();
    8. static QString errorString();
    9. protected:
    10. static void setErrorString(QString);
    11. private:
    12. static QString m_errorString;
    13. };
    14. ...
    To copy to clipboard, switch view to plain text mode 

    But when I try to access from this functions to the var m_errorString the compiler say that the var isn't defined.

    What can be the problem?

    Thank's

  2. #2
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Accessing to a static variable from the same class

    I don't see an error in the code you posted. That means that the error is somewhere else. A typo, perhaps? Maybe your function definition doesn't have "ObjectDatabase::" in front of the name?

    If that doesn't help, could you copy/paste the code where you try to access the variable?
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Accessing to a static variable from the same class

    Define the static variable in the .cpp file:
    Qt Code:
    1. QString ObjectDatabase::m_errorString;
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  4. The following user says thank you to jpn for this useful post:

    xgoan (1st March 2007)

  5. #4
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Accessing to a static variable from the same class

    Oh, you're right. I admit I've never used static variables in C++ in that way. How silly.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  6. #5
    Join Date
    Jul 2006
    Posts
    126
    Thanks
    17
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Accessing to a static variable from the same class

    So the code that works is this:

    objectdatabase.h
    Qt Code:
    1. ...
    2. class ObjectDatabase{
    3. public:
    4. static bool openDatabase(QString);
    5. static bool createTables();
    6. static void closeDatabase();
    7. static bool vacuumDatabase();
    8. static QString errorString();
    9. protected:
    10. static void setErrorString(QString);
    11. private:
    12. static QString m_errorString;
    13. };
    14. ...
    To copy to clipboard, switch view to plain text mode 

    objectdatabase.cpp
    Qt Code:
    1. ...
    2. QString ObjectDatabase::m_errorString;
    3. ...
    4. void ObjectDatabase::setErrorString(QString string){
    5. m_errorString=string;
    6. }
    7. ...
    To copy to clipboard, switch view to plain text mode 

    Thanks to all

  7. #6
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: Accessing to a static variable from the same class

    Now we could start a discussion, whether the whole static stuff is good design, or if it is better be done as a singleton. Classes with nothing else but static methods and static members always make me a bit nervous.

  8. #7
    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: Accessing to a static variable from the same class

    You can always convert such class to a namespace with functions and global objects if you don't like a static class :]

Similar Threads

  1. Replies: 16
    Last Post: 23rd May 2008, 11:12
  2. Static functions and class members
    By Raistlin in forum General Programming
    Replies: 5
    Last Post: 22nd December 2006, 11:00
  3. Problem regarding DLL
    By vermarajeev in forum General Programming
    Replies: 3
    Last Post: 5th October 2006, 04:57
  4. Replies: 2
    Last Post: 4th May 2006, 20:17
  5. Is there the static class in C++?
    By gtthang in forum General Programming
    Replies: 4
    Last Post: 25th February 2006, 06:46

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.