C++ – What does it mean to have an undefined reference to a static member

cc++-faq

I just wrote a class with some static data members, but now I am getting errors about "undefined references". Why doesn't this work? What am I doing wrong?


_(Note: This is meant to be an entry to [Stack Overflow's C++ FAQ](https://stackoverflow.com/questions/tagged/c++-faq). If you want to critique the idea of providing an FAQ in this form, then [the posting on meta that started all this](https://meta.stackexchange.com/questions/68647/setting-up-a-faq-for-the-c-tag) would be the place to do that. Answers to that question are monitored in the [C++ chatroom](https://chat.stackoverflow.com/rooms/10/c-lounge), where the FAQ idea started out in the first place, so your answer is very likely to get read by those who came up with the idea.)_

Best Answer

To understand this, you should have a good understanding of compiling and linking, and the differences between declarations and definitions.


Consider the following class:

//In header file
class Example {
    static bool exampleStaticMember;
};

Here, exampleStaticMember is declared but not defined. This means that if exampleStaticMember is used in a way that means that it must have an address then there must be a separate definition for it. In general, no declaration of a static data member in a class definition is a definition of that member.

The required declaration is usually put in the cpp file which contains the other definitions for the members of the class. It must be in the same namespace as the class definition. The definition typically looks like:

//In source file:
//This may optionally have an initialiser (eg "= true")
bool Example::exampleStaticMember; 

The definition can be put in any cpp file, but it should not be put in the header with the class, because that would be likely to break the One Definition Rule.

As a special case, if the static member variable is an const integral or enumeration type then it can have an initialiser in the class definition:

//In header file
class Example {
    static const int initialised = 15;
};

In this case, the definition in the cpp file is still required, but it is not allowed to have an initialiser:

//In source file
//Note: no initialiser!
const int Example::initialised;

Static members that have been initialised like this can be used in constant expressions.

Templates

For a static data member of a template, things are slightly different. The static member should be defined in the header along with the rest of the class:

//In header file
template<typename T>
class Example {
    static int exampleInt;
    static T exampleT;
}
template<typename T> int Example<T>::exampleInt;
template<typename T> T Example<T>::exampleT;

This works because there is a specific exception to the One Definition Rule for static data members of class templates.

Other uses of static

When the static keyword is applied to functions and objects that are not in a class scope it can take on a very different meaning.

When applied to objects in a function scope, it declares an object that is initialised in the first execution of the function and that subsequently keeps its value between function calls.

When applied to objects or functions at namespace scope (outside of any class or function definition), it declares objects or functions with internal linkage. This usage is deprecated for objects, as the unnamed-namespace provides a better alternative.