Undefined reference to * * * The link error pattern is always endless (more), this time is not found in the class members.
Example 1:undefined reference to VS. Class static member variable
Class A and Class B are declared in file A.h:
Class A
{
Friend class B;
static int pa; Note: The member variable is declared static here
}
Class B {
Public
void Funb (); Funb () used to a::p A;
}
Problem:
GCC return link error: Undefined reference to ' A::p a '. However, by declaring the PA in class B as a non static variable, you can compile it.
WHY:
Review the static data members in class first.
[1] A single piece of storage for a static data member, regardless the how many objects of this class you create.
[2] The static data belongs to the class. The IT name is scoped inside the class and all objects share this data member.
[3] note:the linker would be a error if a static data member was declared but not defined.
So the problem is: in class, you can't replicate static data member, even if you assign a static data member in a constructor, linker still complains.
Because static data member does not belong to any object, even when an object is created, it can only be explained that the object is assigned a value to this data member.
Therefore, the undefined reference to * * * is not found in the definition of class static member variables.
Workaround:
The definition must occur outside the class (no inlining is allowed), and only one of the definition is allowed.
It is common to put it in the implementation file for the class.
In fact, the static member variable is assigned to the global location.
In the above example, in the file A.cpp, in addition to the other function definitions, including the constructor, plus the definition of the PA, there is no need to add a static prefix, but to add Class name qualification, you can not assign a value.
A * B::p A;