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。如許就會導致在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”
…..
深圳北大青鳥