& Quot; unresolved external symbol & quot ;:

Source: Internet
Author: User

Why are classes with static data members getting linker errors?


Because static data members must be explicitly defined in exactly one compilation unit. If you didn't do this, you'll probably get an "undefined external" linker error. For example:

// Fred. h

Class Fred {
Public:
...
Private:
Static int j _; // Declares static data member Fred: j _
...
};
The linker will holler at you ("Fred: j _ is not defined") unless you define (as opposed to merely declare) Fred: j _ in (exactly) one of your source files:
// Fred. cpp

# Include "Fred. h"

Int Fred: j _ = some_expression_evaluating_to_an_int;

// Alternatively, if you want to use the implicit 0 value for static ints:
// Int Fred: j _;
The usual place to define static data members of class Fred is file Fred. cpp (or Fred. C or whatever source file extension you use ).

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.