Home >Backend Development >C++ >Why Does a Static Const Integer Member in C Cause an 'Undefined Reference' Linker Error?

Why Does a Static Const Integer Member in C Cause an 'Undefined Reference' Linker Error?

DDD
DDDOriginal
2024-12-10 20:28:11702browse

Why Does a Static Const Integer Member in C   Cause an

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn