C + + function duplicate definition, header file duplicate inclusion, mutual inclusion problem

Source: Internet
Author: User


1, avoid the header file repeat contains


TEST.H This macro means that if a macro variable is defined Test_h_ it is no longer defined #ifndef test_h_#define test_h_//header file Contents #endif

Or

Test.h tells the precompiled compiler to include only one #pragma once


2. Avoid duplicate definitions


Do not put the function definition in the header file, the correct way is to put the header file function declaration, the source file placement function definition. Because the source file is compiled separately, if the header file has a function definition, each source file containing the header will define the corresponding function, resulting in a duplicate definition. However, there are several exceptions: the definition of an inline function, the definition of a class, the const and static variables, and the template class/function requirement that the header file must contain a definition before it can be templated.


3. Avoid the inclusion and circulation of header files


Header files containing each other lead to recursive inclusion, the logic of the dependency of the header file itself is wrong and cannot be implemented, the header file loop contains the same (such as A.H contains B.h,b.h contains c.h,c.h contains a.h). The correct way is to minimize the dependency of the header file, try to include the header file in the source file, the header file as far as possible with the predecessor declaration (use pointers and references as far as possible, the relevant definitions are placed in the source file medium). A pre-declared class is an incomplete type, and an incomplete type can only be used in a limited way. An object of this type cannot be defined. A non-full type can only be used to define pointers and references to that type, or to declare (rather than define) a function that uses that type as a formal parameter type or return type.


This article is from the "Haopeng Li blog" blog, please be sure to keep this source http://lh2debug.blog.51cto.com/12490788/1890170

C + + function duplicate definition, header file duplicate inclusion, mutual inclusion problem

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.