筆記 解決檔案互相包含問題的小方法

2021-04-13 04:29:30 字數 916 閱讀 2520

兩個標頭檔案互相包含,具體如下:

//------a.h ------

#ifndef _a_

#define _a_

#include "b.h"

typedef struct _node

node, *pnode;

class a

;#endif

//------b.h ------

#ifndef _b_

#define _b_

#include "a.h"

class b

;#endif

//------main.cpp ------

#include "a.h"

#include "b.h"

int main()

編譯時有錯誤,具體錯誤記不清了,就是一些檔案包含重定義或者什麼變數沒有定義的錯誤,現在經過修改後編譯通過:

//------a.h ------

#ifndef _a_

#define _a_

//#include "b.h" //修改處1

class b;   //修改處2

typedef struct _node

node, *pnode;

class a

;#endif

//------b.h ------

#ifndef _b_

#define _b_

//#include "a.h" //修改處4

class a;   //修改處5

class b

;#endif

//------main.cpp ------

#include "a.h"

#include "b.h"

#include

int main()

標頭檔案互相包含問題

當我們有兩個類的標頭檔案互相包含時,如果出現乙個類中有另乙個類的物件時,vs就會報這樣的錯error c4430 缺少型別說明符 假定為 int。test2.h ifndef test2 h define test2 h include include test1.h using namespace...

標頭檔案互相包含解決 關於多檔案中的重複定義標頭檔案

先上錯誤 標頭檔案 fenshu.h pragma once include include using namespace std class fenshu private int a,b public fenshu int x,int y a x b y void display void yf...

關於C 中的標頭檔案互相包含

在c 中一般的程式都會分成標頭檔案和cpp檔案,然後包含不同的標頭檔案可以獲得標頭檔案中的函式的引用,但是這裡就會出現乙個問題就是如果兩個檔案中同時包含了同乙個標頭檔案,例如 a.h中包含了c.h 然後在b.h中 也包含c.h 那麼就會出現上述所提到的重複包含的情況。所以在這裡可以使用乙個 ifnd...