Search code examples
c++pointersstatic-variables

Pointer to static member variable


Possible Duplicate:
C++: undefined reference to static class member

The following C++ code compiles well (using g++ -c) but it doesn't link giving the error: undefined reference toAbc::X'`

#include <iostream>

using namespace std;

class Abc {

public:
    const static int X = 99;
};

int main()
{
    Abc a1;
    cout << &(Abc::X) << endl;
}

I want to know why this is not allowed?


Solution

  • If the static member is used in a way which requires an lvalue (i.e. in a way that requires it to have an address) then it must have a definition. See the explanation at the GCC wiki, which includes references to the standard and how to fix it.