0

Why is it giving me a linking error? I think it's OK to access static members with this->x. Logically it sounds OK. I guess an instance pointer can access what a class owns as per OOPS concepts.

Oak
  • 26,231
  • 8
  • 93
  • 152
sandeep bisht
  • 111
  • 12
  • 6
    I assume you have *declared* the static member variable, but have you *defined* it? – Some programmer dude Apr 03 '14 at 09:20
  • Yes i did declared it and if i remove static keyword it works fine.i haven't initialised it though.it gets initialised to zero by default i guess. – sandeep bisht Apr 03 '14 at 09:22
  • could you please show us the code? – jsantander Apr 03 '14 at 09:25
  • 2
    @sandeepbisht: Joachim asked whether you _defined_ it. You didn't answer that. – Lightness Races in Orbit Apr 03 '14 at 09:26
  • error is unresolved external symbol "public:static int Access::x".And it goes when i remove static – sandeep bisht Apr 03 '14 at 09:28
  • worked for me in http://ideone.com/clAeSU – jsantander Apr 03 '14 at 09:28
  • 1
    Do you put this line `int some_class::v;` in cpp file, not header file. – BlackMamba Apr 03 '14 at 09:37
  • i got it . I didnt defined it in .ccp file where i was accessing it .but i cant find out why i have to define it again in .cpp.I declared it does it not allocates memory ? Can't linker find it out in class declaration. – sandeep bisht Apr 03 '14 at 09:46
  • "I declared it does it not allocates memory ?" - ummm... no. Now you know what the problem is, why don't you do some background reading instead of asking yet again if it really isn't meant to work...? ;-o – Tony Delroy Apr 03 '14 at 10:03
  • "A declaration is a definition unless it declares a function without specifying the function's body, it contains the extern specifier or a linkage-specification and neither an initializer nor a function-body, it declares a static data member in a class declaration, it is a class name declaration, or it is a typedef declaration, a using-declaration, or a using-directive."....i got it – sandeep bisht Apr 03 '14 at 10:05

2 Answers2

6

You need to also define the static member variable. For example:

// in .h
class some_class {
    static int v;  // it's just a declaration
};

// in .cpp
int some_class::v; // here's the defination
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
1

Put declarations in your Foo.h file:

class Foo {
    static int v;
};

Put defenitions in your Foo.cpp file:

int Foo::v;

Here are some explanations: Why the static data members have to be defined outside the class separately in C++, Defining static members in C++

I find it helpful to treat linking errors differently. If you do something against the rules of the language -- the compilation error will be issued, so if you get a linking error it must be not something that you've done against the rules of the language directly, rather than you've omitted something. And usually the error message tells you what exactly you have omitted.

Community
  • 1
  • 1
Anton Daneyko
  • 6,528
  • 5
  • 31
  • 59
  • It seems to me that static class members are more like global variables that have their names scoped to the class. If I want such a variable I normally put it in Foo.cpp but don't make it a static class member, nobody else can see that variable anyway. – M.M Apr 03 '14 at 10:08