Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean to have an undefined reference to a static member?

Tags:

c++

c++-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.)_
like image 920
Mankarse Avatar asked Aug 17 '11 12:08

Mankarse


People also ask

How do you fix undefined references in C++?

You can fix undefined reference in C++ by investigating the linker error messages and then providing the missing definition for the given symbols. Note that not all linker errors are undefined references, and the same programmer error does not cause all undefined reference errors.

How do you declare a static member variable in C++?

When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member. A static member is shared by all objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present.

How do you define a static member?

Static members are data members (variables) or methods that belong to a static or a non static class itself, rather than to objects of the class. Static members always remain the same, regardless of where and how they are used.

Why is static member variable not defined inside the class?

That's because the static class member is stored separately rather than as part of an object. The exception to the initialization of a static data member inside the class declaration is if the static data member is a const of integral or enumeration type.


2 Answers

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.

like image 146
Mankarse Avatar answered Sep 19 '22 00:09

Mankarse


You have to instantiate static members defined in a header in a .cpp file. For example:

// foo.h

class foo {
    static int X;
};


// foo.cpp

#include "foo.h"

int foo::X = 0;
like image 21
Constantinius Avatar answered Sep 20 '22 00:09

Constantinius