-3

I wrote a program with static variable. However, the I am getting the following error :

[Linker error] C:/Users/prcm/Documents/Practice/junk.cpp:8: undefined reference to `X::a'

Here is the code

#include <iostream>

using namespace std;

class X {
    public:     
        static const int a;

    public:
        static int getA() { return a; }    
};

int main() {     
    cout<< X::getA()<< endl;
    return 0;
}
Okku
  • 7,468
  • 4
  • 30
  • 43
Prashanth Cm
  • 151
  • 2
  • 4
  • 8
  • You haven't made an instance of X, nor is it static. – ross Jul 05 '15 at 15:15
  • Well, the error is correct: you never defined `X::a`. Which C++ book are you using? – Lightness Races in Orbit Jul 05 '15 at 15:18
  • @πάντα ῥεῖ That was not even nearly a duplicate. Once more, please be cautious and responsible with your dupehammer. – Lightness Races in Orbit Jul 05 '15 at 15:19
  • @LightnessRacesinOrbit It well refers to the [_static member initialization_](http://stackoverflow.com/questions/23767241/c-static-const-class-members-initialization) missing definition problem. Not the best dupe may be, but answered the question properly. Read dupes twice please. – πάντα ῥεῖ Jul 05 '15 at 15:20
  • @πάνταῥεῖ: What does this question have to do with initializers? Or with the error message in that other question? Hint: _nothing_! The two questions are totally different. Read dupes thrice please. – Lightness Races in Orbit Jul 05 '15 at 15:25
  • @LightnessRacesinOrbit As mentioned, not well selected regarding the OP topic. Still the (accepted) answer solves the problem. – πάντα ῥεῖ Jul 05 '15 at 15:36
  • @πάνταῥεῖ: "Not well selected"? The new dupe _specifically exists for this situation_. Please stop trolling; it's tiresome. – Lightness Races in Orbit Jul 05 '15 at 15:37
  • @LightnessRacesinOrbit I'm not really _trolling_, and you well know. – πάντα ῥεῖ Jul 05 '15 at 15:38
  • @πάνταῥεῖ: I was generously assuming you must be trolling, since from the past I seem to recall a higher level of competence on your part. – Lightness Races in Orbit Jul 05 '15 at 15:38
  • @LightnessRacesinOrbit _"Let us not.​​​​ "_ Of course not (misclicked). I'm just tired and a bit sick. May be you're right I could expose much better competence as usual. – πάντα ῥεῖ Jul 05 '15 at 15:40
  • @LightnessRacesinOrbit Just one more word: The actual problem description is a bit hard to find in the dupe you've linked now, since there are so many situations described. I've been using another _"canonical"_ for this kind of question earlier: [Can't set value of static object field (error LNK2001: unresolved external symbol)](http://stackoverflow.com/questions/23983916/cant-set-value-of-static-object-field-error-lnk2001-unresolved-external-symbo) – πάντα ῥεῖ Jul 05 '15 at 15:47
  • @πάνταῥεῖ Oh no OP would have to _read_!!! Whatever shall we do – Lightness Races in Orbit Jul 05 '15 at 16:03
  • @LightnessRacesinOrbit _"Whatever shall we do "_ Starting an anti-analphabetism initiative? ;-) ... – πάντα ῥεῖ Jul 05 '15 at 16:05

1 Answers1

0

It's right, you have never defined X::a.

Put the following line after the class declaration: const int X::a = 0;

skypjack
  • 49,335
  • 19
  • 95
  • 187
  • _"Put the following line after the class declaration: ..."_ That might not work inside header files, leading to _multiple symbol definition_ errors. It should be ensured that this statement appears in a unique translation unit. – πάντα ῥεῖ Jul 05 '15 at 15:34
  • 1
    I was referring to his example, that really looks like a monolithic file. Putting that line after the class declaration works there. – skypjack Jul 05 '15 at 15:36
  • I've just put a warning, didn't say you're wrong. – πάντα ῥεῖ Jul 05 '15 at 15:49
  • Yeah, absolutely, I'm not upset. I was explaining for the readers why I've replied that way. I've still to learn how to reply on stackoverflow, so these suggestions are welcome. Thank you for the warning!! ;-) – skypjack Jul 05 '15 at 15:51
  • But the same code works if initialize 'a' within class itself. like this-> static const int a=1; so i was expecting the output with default value of 'a'. – Prashanth Cm Jul 05 '15 at 16:23
  • @PrashanthCm: Even then you are still required to write a _definition_ for `a`. It's just that your compiler may not reject the program if you don't, due to various things. Doesn't mean it's right. – Lightness Races in Orbit Jul 05 '15 at 17:00