Undefined Reference to Static Class Member in C++

Undefined Reference to Static Class Member in C++

In C++ programming, you must be getting these undefined reference errors while dealing with the static class members, which occurs due to not defining the static class properly, and to fix these errors, you can use the common fixes according to the error you are facing in your program. In this article, we will discuss the static member in a class, undefined reference, common causes of error, and how to fix it in C++.

Table of Contents:

Static Members in a Class

Static Members in a Class

In C++, a static member of a class is a variable or function that belongs to the class itself rather than to any individual object of the class. This means:

  • A static variable is shared across all objects of the class, meaning there is only one copy of it in memory.
  • A static function can only access other static members and does not operate on specific instances.

Declaring a Static Member

Static members must be declared inside the class and then defined outside the class.

Example:

Cpp

Output:

Declaring a Static Member

Explanation: The code shows how a static class member in a C++ program with the variable count is declared inside MyClass and also defined outside to allocate storage and proper linking.

Undefined Reference Error in C++

An undefined reference error comes up any time the linker cannot find the definition of a function or variable that has been declared and used in the program. Thus, unlike a compilation error, which stops the program from being compiled, an undefined reference indicates that the linking phase could not find an implementation of a product that was recognized by the compiler inside the source file.

Example:

Cpp

Output:

Undefined Reference Error

Explanation: The code shows how a function, myFunction(), is declared but is not defined, which leads to an undefined reference error during the linking phase because the linker doesn’t find its implementation.

Common Causes of Undefined Reference in Static Class Members in C++

Below are a few common causes of undefined references in static classes in C++

  • When the static variable is declared but not defined outside the class in the C++ program then the undefined reference will occur. 
  • In the previous version of C++17, static const integral members must be defined outside the class if referenced, otherwise, the linker throws an “undefined reference” error, but the C++17+ removes this requirement.
  • If the static member function is declared in the code but not properly defined, then an undefined reference will occur.
  • When the static variable is defined in a header file without an inline function, it will cause multiple definitions.
  • If the static members of template classes are not defined in the header file, then an undefined reference will occur.

Get 100% Hike!

Master Most in Demand Skills Now!

How to Fix Undefined Reference for Static Class Members in C++

Below are a few methods to fix the undefined reference for static class members in C++:

1. Define the Static Member Outside the Class

A static variable must be declared inside the class and defined outside to allocate storage.

Example:

Cpp

Output:

Define the Static Member Outside the Class

The code shows how an undefined reference error in the C++ program occurs because the static member value is declared, but it is not defined outside the class. 

Fixed code:

Cpp

Output:

Define the Static Member Outside the Class Fixed Code

The code shows how a static class member with the static variable value is declared inside the class Myclass and defined outside the class for allocating the storage and proper linking.

2. Define Static Const Integral Members

Before C++17, static const integral members must be defined outside the class if they are referenced.

Example:

Cpp

Output:

Define Static Const Integral Members

The code shows that in C++ versions before C++17, this code causes an undefined reference error because the value is a static const integral member that is declared but not defined outside the class. To fix this, add const int MyClass::value; outside the class. C++17 and later remove the need for this external definition.

Fixed code:

Cpp

Output:

Define Static Const Integral Members Fixed Code

The code shows that the program correctly defines a static const integral member. In C++ versions before C++17, an external definition (const int MyClass::value;) is required if the member is used (e.g., taking its address). C++17 and later remove this requirement, allowing in-class initialization to suffice.

3. Properly Define Static Member Functions

A static function must be declared inside the class and defined outside with ClassName::.

Example:

Cpp

Output:

Properly Define Static Member Functions

The code shows an undefined reference error at the linking stage because the static member function showMessage() is declared but not defined. To fix this, provide its definition outside the class.

Fixed code:

Cpp

Output:

Properly Define Static Member Functions Fixed Code

The code shows that the C++ program correctly implements a static member function as the function showMessage() is declared inside the class and defined outside, ensuring proper linkage. Since static member functions do not require an instance of the class, they are called directly using MyClass::showMessage().

4. Use an inline for Static Members in C++ Header Files

The inline allows the definition of the static members inside the class in a header file which makes sure that only one definition is there across the translation units, so inline must be used.

Example:

Cpp

Output:

Use an inline for Static Members in C++ Header Files

The code shows how a multiple definition error occurred because the static member value is defined in a header file (myclass.h), and when it is included in multiple translation units, it breaks the One Definition Rule (ODR).

Fixed code:

Cpp

Output:

Use an inline for Static Members in C++ Header Files Fixed Code

The C++17+ code uses inline to define value inside the class, ensuring a single definition across translation units and preventing multiple definition errors.

5. Define Static Template Members in the Header File

For template classes, static members must be defined in the header file.

Example:

Cpp

Output:

Define Static Template Members in the Header File

The code shows how a template class, MyClass, causes a linker error because the static member value is declared, but it is not defined. 

Fixed code:

Cpp

Output:

Define Static Template Members in the Header File Fixed Code

The code shows how a static member in a template class with a static variable value is declared inside the class and defined in the header file to ensure proper instantiation for each template type.

Conclusion

Understanding static class members in C++ is important to prevent undefined reference errors during linking. The common reason for these errors is not defining the static members outside the class. Pre-C++17 required explicit definitions for static const integral members, but this is no longer necessary in C++17 and later. Also, properly defining the static member variables, static functions, and static template members will ensure compilation and linking. Using inline for static members in header files (C++17+) also prevents multiple definition errors. By understanding how to fix them, you can easily avoid common errors and write efficient, error-free C++ programs.

FAQs on Undefined Reference to Static Class Members in C++

Q1. Why do I get an "undefined reference" error for a static member?

Static members must be defined outside the class, as only declaration alone is not enough.

 

Q2. Do static const integral members need an external definition?

Pre-C++17: Yes, if referenced. C++17+: No, an external definition is not required.

Q3. Can static members be defined in a header file?

Yes, you can define static members in a header file, but use inline (C++17+) or define them in a .cpp file to prevent multiple definitions.

Q4. How to define static members in a template class?

You can define static members inside the header file, as templates are instantiated at compile time.

Q5. Are static members inherited in C++?

Yes, static members are inherited in C++, but they remain tied to the base class unless it is overridden.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner