Variables cannot be defined in header files.

Source: Internet
Author: User
Note that variables cannot be defined in header files !!! Generally, only variables are declared in the header file, because the header file

To be included by other files (I .e. # include), if you put the definition in the header file, you cannot avoid defining variables multiple times,

C ++ does not allow multiple definitions of variables. A program only defines the specified variables once and can be declared countless times.

However, there are three exceptions. The definition of the third entity can also be placed in the header file.

1. Definitions of known const variables at compilation can be put into the header file, for example, const int num (10 );
2. class definitions can be placed in header files.

3. inline functions

4. The new constexpre int num (10) in C ++ 11 should be okay.

These three entities can be defined in multiple source files, as long as the definitions in each source file are the same.

Variables cannot be defined in header files.

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.