class Foo
{
static void Method1();
static int diddly;
}
void Foo::Method1()
{
diddly = 5;
}
This results in a link error: unresolved external symbol "public: static int Foo::diddly" (?diddly@Foo@@2HA)
But I thought static methods were able to access static member variables? What am I doing wrong?
Thanks for the help
diddly
Static class member variables and functions
After looking everywhere, I''m hoping you guys can help me with a problem I''ve been having. I''ll just post some code.
Try adding the line
somewhere in the source. Does that help at all?
Peace,
Brendon
int Foo::diddly = 0;
somewhere in the source. Does that help at all?
Peace,
Brendon
Where do you mean? I''ve tried accessing it with Foo::diddly before, and it doesn''t work.
diddly
diddly
In the source file along with the method definitions. Just put it in as if you were declaring a global variable, but with a special name that puts it in the class: ie Foo::diddly
I hope that made more sense,
Doc
I hope that made more sense,
Doc
The reason for this is that for each static variable, there has to be exactly one implementation. So the implementation can''t be in the header file, since it''s included multiple times.
Don''t listen to me. I''ve had too much coffee.
Don''t listen to me. I''ve had too much coffee.
quote:Original post by Sneftel
The reason for this is that for each static variable, there has to be exactly one implementation. So the implementation can''t be in the header file, since it''s included multiple times.
Agreed. But do you know why the compiler has no problem with static variables inside inlined functions?
struct A{ void f(){ static int i; i = 3; }};
Cédric
Inside inlined functions or any function whatsoever. This is because the compiler treats that variable as a global variable with scope limited to that function. For example:
void foo()
{
int A;
static int B;
}
A will be created on the stack of the function.
B will be created in the global space of the program. No matter when you enter this function, B will retain its value, unlike A which is destroyed whenever you leave the scop of foo().
void foo()
{
int A;
static int B;
}
A will be created on the stack of the function.
B will be created in the global space of the program. No matter when you enter this function, B will retain its value, unlike A which is destroyed whenever you leave the scop of foo().
Umm... On second thought, there is one instance of the static variable (inside the inlined function) / object, so it must be allocated at the same time as the object, right?
Cédric
[edited by - cedricl on November 7, 2002 8:57:57 PM]
Cédric
[edited by - cedricl on November 7, 2002 8:57:57 PM]
Sorry, I think I was unclear.
in foo.h
in foo.cpp
Or did I miss what you were saying entirely? I thought that static member functions could access static member variables. I don''t understand why it''s giving me that error message. Why a link error? Thanks for all the help so far.
diddly
in foo.h
class Foo{ static void Method1(); static int diddly;}
in foo.cpp
void Foo::Method1(){ diddly = 5;}
Or did I miss what you were saying entirely? I thought that static member functions could access static member variables. I don''t understand why it''s giving me that error message. Why a link error? Thanks for all the help so far.
diddly
You missed what they were saying . No worry though, static class members and variables are a hairy topic.
I believe this is what you are trying to do:
Which compiles with no warnings/errors. The reason you have to stick that "int Foo::diddly;" in there is that static class members must be initialized only once, so you have to declare them inside of a translation unit (ie. a source file, not a header file) so the compiler knows where to look to get the data for that static object.
I believe this is what you are trying to do:
class Foo{public: static void Method1(); static int diddly;};int Foo::diddly;void Foo::Method1(){ diddly = 5;}int main() {return 0; }
Which compiles with no warnings/errors. The reason you have to stick that "int Foo::diddly;" in there is that static class members must be initialized only once, so you have to declare them inside of a translation unit (ie. a source file, not a header file) so the compiler knows where to look to get the data for that static object.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement