Why should I add # ifndef # define # endif to the header file?

Source: Internet
Author: User

When you use the VC menu to add a class, you will find that the automatically generated code is always like the following:

# If! Defined (afx_xxxx1_included _)

# Define afx_xxxx1_encoded _

Code

# Endif

This is to prevent the header file from being repeatedly contained. Repeated inclusion can be illustrated in the following example: for example, there is a header file. h, which contains a function FA and another header file B. h. The function FB is provided. If fa is required for FB implementation, B. h must contain. h; if a function in the CPP file needs to use FA and Fb, it must contain. H and B. h,. h. Compile the program with the following error:

Error c2084: function 'bool _ cdecl FA () 'already has a body

The solution is to add the following in a. h:

# Ifndef

# Define

Original code

# Endif

 

The sample source code is as follows:

// A. h

# Ifndef

# Define

 

Bool aorb (bool)

{

Return;

}

 

# Endif

 

// B. H

# Include "A. H"

 

Bool cord (bool)

{

Return aorb ();

}

 

// A. cpp

# Include "A. H"

# Include "B. H"

 

Int main ()

{

Bool a = 0;

Bool B = aorb ();

Bool c = cord (B );

 

Getchar ();

Return 0;

}

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.