The static variable in the class does not define a solution that causes the undefined reference to static class member problem

Source: Internet
Author: User
Tags static class
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;


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.