Home >Backend Development >C++ >Why Does a Static Const Integer Member in C Cause an 'Undefined Reference' Linker Error?
Undefined Reference to Static Const Integer Member
Despite the misconception that C permits defining static const member integers within class definitions, the given code triggers a linker error due to an undefined reference to test::N. This error arises when the code attempts to use the uninitialized static member variable.
While it is possible to initialize static const integers in the class declaration, this does not constitute a definition. To resolve the issue, the member must be defined separately in the namespace scope, but without an initializer.
In this case, since std::min takes its parameters by constant reference, a definition of test::N is required. Without the definition, the linker cannot resolve the reference to the member variable.
The above is the detailed content of Why Does a Static Const Integer Member in C Cause an 'Undefined Reference' Linker Error?. For more information, please follow other related articles on the PHP Chinese website!