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:
WhateverClass.cpp:47: undefined reference to `Messages::PluginManagerInitComplete'
WhateverClass.cpp:47: undefined reference to `Messages::PluginManagerInitComplete'
To copy to clipboard, switch view to plain text mode
Here is the class code:
Messages.h
----------------
#ifndef MESSAGES_H
#define MESSAGES_H
class Messages
{
public:
Messages();
static int WebViewCreated;
static int WebViewShown;
static int LinkClicked;
static int JavaScriptCleared;
static int PluginManagerInitComplete;
static int PluginManagerUnload;
};
#endif // MESSAGES_H
Messages.h
----------------
#ifndef MESSAGES_H
#define MESSAGES_H
class Messages
{
public:
Messages();
static int WebViewCreated;
static int WebViewShown;
static int LinkClicked;
static int JavaScriptCleared;
static int PluginManagerInitComplete;
static int PluginManagerUnload;
};
#endif // MESSAGES_H
To copy to clipboard, switch view to plain text mode
And:
Messages.cpp
-----------------
#include "Messages.h"
int Messages::WebViewCreated = 1;
int Messages::WebViewShown = 2;
int Messages::LinkClicked = 3;
int Messages::JavaScriptCleared = 4;
int Messages::PluginManagerInitComplete = 5;
int Messages::PluginManagerUnload = 6;
Messages::Messages()
{
}
Messages.cpp
-----------------
#include "Messages.h"
int Messages::WebViewCreated = 1;
int Messages::WebViewShown = 2;
int Messages::LinkClicked = 3;
int Messages::JavaScriptCleared = 4;
int Messages::PluginManagerInitComplete = 5;
int Messages::PluginManagerUnload = 6;
Messages::Messages()
{
}
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
Bookmarks