M
Michael B. Trausch
Hello everyone,
I am having a problem with static class properties that I suspect is somewhat
trivial, but I don't understand what is wrong. I am using GNU C++ on
Windows, though I am (hoping) to make this program cross-platform using the
wxWidgets library and not using any functionality that isn't commonly
supported. Anyway, the problem is that I want to have a class that has two
variables and two methods that operate on the class itself, and not an
instance of it. Here is the class declaration:
class TmudConfiguration {
private:
static bool InitState;
static TmudConfiguration *inst;
public:
TmudConfiguration();
};
I have this in the .h file for the class. In the .cc file for the class, I
have code that refers to all of these things, but every time the linker finds
a reference to InitState or TmudConfiguration, it tells me that it is an
"undefined reference to TmudConfiguration::InitState" or the other. A snippet:
TmudConfiguration::TmudConfiguration() {
if(TmudConfiguration::InitState == true) {
delete this;
} else {
TmudConfiguration::InitState == true;
}
}
I don't quite understand why that doesn't work. If I remove the
TmudConfiguration:: prefix from InitState, it does the same thing. Examples
that I've looked at from the Internet seem to show that the variable just gets
accessed as if it were a locally scoped variable within a function and they work.
Interestingly enough, though, I just tried to compile an example that did not
work, either (obtained from
http://www.cprogramming.com/tutorial/statickeyword.html). Though, I can't
find anything in what I am doing that differs from that or any other example,
really. :-/
Can someone help me to understand what I am doing wrong?
-- Mike
I am having a problem with static class properties that I suspect is somewhat
trivial, but I don't understand what is wrong. I am using GNU C++ on
Windows, though I am (hoping) to make this program cross-platform using the
wxWidgets library and not using any functionality that isn't commonly
supported. Anyway, the problem is that I want to have a class that has two
variables and two methods that operate on the class itself, and not an
instance of it. Here is the class declaration:
class TmudConfiguration {
private:
static bool InitState;
static TmudConfiguration *inst;
public:
TmudConfiguration();
};
I have this in the .h file for the class. In the .cc file for the class, I
have code that refers to all of these things, but every time the linker finds
a reference to InitState or TmudConfiguration, it tells me that it is an
"undefined reference to TmudConfiguration::InitState" or the other. A snippet:
TmudConfiguration::TmudConfiguration() {
if(TmudConfiguration::InitState == true) {
delete this;
} else {
TmudConfiguration::InitState == true;
}
}
I don't quite understand why that doesn't work. If I remove the
TmudConfiguration:: prefix from InitState, it does the same thing. Examples
that I've looked at from the Internet seem to show that the variable just gets
accessed as if it were a locally scoped variable within a function and they work.
Interestingly enough, though, I just tried to compile an example that did not
work, either (obtained from
http://www.cprogramming.com/tutorial/statickeyword.html). Though, I can't
find anything in what I am doing that differs from that or any other example,
really. :-/
Can someone help me to understand what I am doing wrong?
-- Mike