通常在使用VC进行函数定义时会指定该函数调用方式,诸如: -电脑资料

电脑资料 时间:2019-01-01 我要投稿
【www.unjs.com - 电脑资料】

   

    假定有以下几个头文件及其包含关系为:

    File1.h,file2.h,file3.h,file4.h,file5.h,main.cpp

    那么:file3.h包含file1.h,file2.h,file4.h包含file1.h,file2.h,file5.h包含file3.h,file4.h,

通常在使用VC进行函数定义时会指定该函数调用方式,诸如:

。如许就会导致在file5中对file1和file2的反复包含,编译时就会报错。

    解决方法:

    1:应用#ifndef

    #define

    #endif

    即每个文件在定义时都写成以下情势(以file1.h为例):

    #ifndefH_FILE1

    #defineH_FILE1

    #include

    #include

    …..

    #endif

    File3.h:#ifndefH_FILE3

    #defineH_FILE3

    #include

    #include

    #inlcude”file1.h”

    #include”file2.h”

    …..

    #endif

    方法二:在每个文件的头部定义:#pragmaonce(用于解释本文件中的内容只应用一次)

    例:fiel1.h:

    #pragmaonce

    #include

    #include

    …..

    File3.h:

    #pragmaonce

    #include

    #include

    #include”file1.h”

    …..

最新文章