Undefined reference to static variable
You only declared A::i
, need to define A::i
before using it.
class A
{
public:
static int i;
static void init(){
i = 1;
}
};
int A::i = 0;
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
A::i = 0;
A::init();
return 0;
}
Also your init() function should return a value or set to void.
Undefined reference to declared C++ static member variable
In C++, static
variables are essentially syntactic sugar around global variables. Just like global variables, they must be defined in exactly one source file, with:
int Test::nb;
and if you want to initialize it with a particular value,
int Test::nb = 5; // or some other expression
C++ Class Method contains Undefined Reference to a Static Variable
You should not use the static keyword when defining the field in the .cpp file, only when declaring it in the class. Note in the code comments you have "declaring" and "defining" the wrong way round.
Additionally, when defining the member, you need to qualify it with the class name.
So the .cpp definition should be:
MyTest::DBGL MyTest::debug_level_d = MyTest::FULL;
By using the static keyword in the definition, you restrict it to internal linkage.
See: http://en.cppreference.com/w/cpp/language/static
Undefined reference to initialized static member variable with make_shared
Since C++17 the first code should work correctly: a static constexpr
class member variable is implicitly inline
which means the compiler takes care of making sure a definition exists .
Prior to C++17 the code has undefined behaviour (no diagnostic required) due to ODR violation. A static class member that is odr-used must also have an out-of-line definition.
Binding a reference to a variable counts as odr-use, and that happens with make_shared<int>(c)
since that function is defined as :
template< class T, class... Args >
shared_ptr<T> make_shared( Args&&... args );
so the argument is bound to a reference parameter.
In theory you should be able to work around it with make_shared<int>(+c)
... then the reference is bound to the temporary result of +c
and not to c
itself, therefore there is no odr-use. Similar theory to your posted workaround in the question.
enum { c = 0 };
is another possible workaround, if the type is int
in the real code .
Related Topics
Enum to String in Modern C++11/C++14/C++17 and Future C++20
Compilers and Argument Order of Evaluation in C++
Is a String Literal in С++ Created in Static Memory
What Is the Best Open Xml Parser For C++
Separating Class Code into a Header and Cpp File
What Happens If You Call Erase() on a Map Element While Iterating from Begin to End
What Are the Differences Between C-Like, Constructor, and Uniform Initialization
How to Get Main Window Handle from Process Id
Why Does the C++ Map Type Argument Require an Empty Constructor When Using []
Error: Free(): Invalid Next Size (Fast):
Accessing Class Members on a Null Pointer
How to Pass a Multidimensional Array to a Function in C and C++
What Breaking Changes Are Introduced in C++11
How to Detect Whether There Is a Specific Member Variable in Class
How to Get the Ip Address of a (Linux) Machine
Sequence-Zip Function For C++11
Multithreading Program Stuck in Optimized Mode But Runs Normally in -O0