tags:

views:

156

answers:

3
#include<stdio.h>

class A { public: int a;};
class B: public A { 
public:
    static int b;
    B(){
        b++;
        printf("B:%d\n",b);
    }   
};

int main() {

    A* a1 = new B[100];
    A* a2 = new B();
    return 0;
}

Error:

In function `main':
undefined reference to `B::b'
undefined reference to `B::b'
undefined reference to `B::b'
undefined reference to `B::b'
+16  A: 

Static variables need to be allocated outside the class. Add this line outside the class B:

int B::b;

Think of static variables as being declared with the extern keyword. They still need to be allocated somewhere. This means the allocation should never be in the header file!

PierreBdR
@PierreBdR: actually, allocation can be there in a header file, if that file is used at-most one place in a program (to avoid linking errors). Am I correct?
Lazer
@Lazer: indeed, but that defeat the purpose of a header file, that is to be included in more than one file.
PierreBdR
@PierreBdR: yeah you are correct, I just wanted to check whether I understood that right! thanks!
Lazer
+3  A: 

Because it is static, you also need to define storage for B::b (in a class definition, all you have done is declared the variable).

You need to add:

int B::b;
R Samuel Klatchko
I disagree, B::b has no value whether it's static or not, that will still fail.
Silmaril89
@Silmaril- huh?
dash-tom-bang
@Silmaril89: "Has no value"? What is it supposed to mean? Objects with static storage duration in C++ are always zero-initialized. In this case `B::b` is guaranteed to be zero at program startup.
AndreyT
@AndreyT: That is not true. PODs are never initialized in C++ unless explicitly specified. In this case, the declaration just allocate the memory, but doesn't initialize it.
PierreBdR
@Silmaril89 - you are incorrect. To quote the spec: 9.4.2/7 - `Static data members are initialized and destroyed exactly like non-local objects (3.6.2, 3.6.3).` And in 3.6.2/1 - `Objects with static storage duration (3.7.1) shall be zero-initialized (8.5)`
R Samuel Klatchko
@PierreBdR - that is only true for automatic variables. Static and global variables are 0 initialized by default.
R Samuel Klatchko
@PierreBdR: Absolutely incorrect. One more time: all objects with static storage duration are always guaranteed to be properly automatically *zero-initialized* before any other initialization begins. It does not matter whether you initialize them explicitly or not.
AndreyT
A: 

You have to initialize your static member in the according .cpp file like int B::b = 0

InsertNickHere
You don't have to initialize objects with static storage duration to `0` - they are guaranteed to be zero-initialized automatically in any case. What is missing in OP's case is the *definition* of the object. Whether it is initialized explicitly does not immediately matter.
AndreyT
Ok, thanks for that clarification.
InsertNickHere
Perhaps you can get away without initializing but it won't hurt and it makes the code clearer.
Noah Roberts