在代碼中,兩個類相互引用的問題,那麼我們就需要在頭文件中相互寫 #include,這樣會造成相互循環cpoy頭文件,編譯器報錯,爲了解決這個問題,設置了前置聲明這個方法。
A.h
#ifndef A_H
#define A_H
class B
class A{
typedef vector<string>::sizetype size_type;
B* b;
}
#endif
B.h
#ifndef B_H
#define B_H
#include "A.h"
class B{
A::size_type num;
}
#endif
前置聲明,解決了在A中可以調用B,但在單元編譯時,無法確定B的size,所以把B定義成B*,固定大小爲系統大小,就解決了。