Results 1 to 6 of 6

Thread: accessing static member variables of one class in another class

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2010
    Posts
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Lightbulb accessing static member variables of one class in another class

    Doh! I have been hammering my head against the wall on this off and on, searching for a solution and none of them worked, finally, 5 minutes after posting this, I tried adding the const keyword and initializing the variables in the header file, and removing the inits from the cpp and it worked...

    I maintain I never would have figured it out if I hadn't posted this...It's the general law of the universe.
    Hi,

    This is more or less a c++ question only, so I am posting it here. It is, in my mind something that should be simple, I am just missing the point.

    I have tried it fifty different ways and none of them work. The problem:

    I need to access global constants in any part of my program. These are basically just message names that I pass around where the value is just an Int.

    What I want it to look like is: Messages::SomeMessageName. This is done alot in say AS3 or PHP, where you define a class that holds a bunch of static variables so you can access them like if (msg == Message::SomeMessageName) ...

    I have tried using just a namespace, I have tried creating a class, I have tried having QtCreator generate me a class based on qObject etc, and Always, I get this error:

    Qt Code:
    1. WhateverClass.cpp:47: undefined reference to `Messages::PluginManagerInitComplete'
    To copy to clipboard, switch view to plain text mode 

    Here is the class code:

    Qt Code:
    1. Messages.h
    2. ----------------
    3. #ifndef MESSAGES_H
    4. #define MESSAGES_H
    5. class Messages
    6. {
    7. public:
    8. Messages();
    9. static int WebViewCreated;
    10. static int WebViewShown;
    11. static int LinkClicked;
    12. static int JavaScriptCleared;
    13. static int PluginManagerInitComplete;
    14. static int PluginManagerUnload;
    15.  
    16. };
    17.  
    18. #endif // MESSAGES_H
    To copy to clipboard, switch view to plain text mode 

    And:

    Qt Code:
    1. Messages.cpp
    2. -----------------
    3. #include "Messages.h"
    4. int Messages::WebViewCreated = 1;
    5. int Messages::WebViewShown = 2;
    6. int Messages::LinkClicked = 3;
    7. int Messages::JavaScriptCleared = 4;
    8. int Messages::PluginManagerInitComplete = 5;
    9. int Messages::PluginManagerUnload = 6;
    10. Messages::Messages()
    11. {
    12. }
    To copy to clipboard, switch view to plain text mode 

    This is probably not how I want to do it, I just tried this and it fails, always the same message, undefined reference. This is how-ish (not exactly) be done is say AS3 or something, but this is obviously not the C++ way, what I am hoping to learn is, how will I go about this is C++?

    Many thanks,
    Jason
    Last edited by jasonknight; 11th June 2010 at 18:18. Reason: stupidity

Similar Threads

  1. Using a static member in a class
    By feraudyh in forum Newbie
    Replies: 4
    Last Post: 29th April 2010, 10:58
  2. Using a QMutex as a static class member
    By emostar in forum Qt Programming
    Replies: 2
    Last Post: 15th June 2009, 13:48
  3. QDevelop debuggng - viewing class member variables
    By dbrmik in forum Qt-based Software
    Replies: 0
    Last Post: 7th January 2009, 10:40
  4. Replies: 3
    Last Post: 19th February 2008, 13:10
  5. Accessing to a static variable from the same class
    By xgoan in forum General Programming
    Replies: 6
    Last Post: 5th March 2007, 10:50

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.