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。如許就會(huì)導(dǎo)致在file5中對(duì)file1和file2的反復(fù)包含,編譯時(shí)就會(huì)報(bào)錯(cuò)。
解決方法:
1:應(yīng)用#ifndef
#define
#endif
即每個(gè)文件在定義時(shí)都寫成以下情勢(shì)(以file1.h為例):
#ifndefH_FILE1
#defineH_FILE1
#include
#include
…..
#endif
File3.h:#ifndefH_FILE3
#defineH_FILE3
#include
#include
#inlcude”file1.h”
#include”file2.h”
…..
#endif
方法二:在每個(gè)文件的頭部定義:#pragmaonce(用于解釋本文件中的內(nèi)容只應(yīng)用一次)
例:fiel1.h:
#pragmaonce
#include
#include
…..
File3.h:
#pragmaonce
#include
#include
#include”file1.h”
…..
深圳北大青鳥