Inherited static variables gives linkage error

less than 1 minute read

If you try to access inherited static variables from upper class, you will end up with errors like

Population.cpp:38: undefined reference to 'std::Common::crrate'

This is because static variables needs to be declared in source code of the using class as well. However, they have to outside of the class definition. Here is an example:

// MyClass.h
class MyClass {
	static int GetVar() {
		return shared_variable;
	static int shared_variable;

// MyClass.cpp
#include "MyClass.h"
int MyClass::shared_variable; // Also do any initialization if needed.

Yep, C++ is a strict language.