[問題] include

看板C_and_CPP (C/C++)作者 (技安)時間16年前 (2010/06/19 01:13), 編輯推噓2(204)
留言6則, 4人參與, 最新討論串2/6 (看更多)
遇到兩個.h檔必須要互相include對方時發生的問題 //a.h #ifndef A_H #define A_H #include "b.h" class A { A(){_b = new B(this);} void print(){} private: B* _b; }; #endif --------------------- //b.h #ifndef B_H #define B_H #include "a.h" class A; class B { public: B(A* ptr){ _a = ptr; _a->print(); } private: A* _a; } ; #endif --------------------- compile error: invalid use of undefined type struct A; forward declaration of 'struct A' 我是不知道該怎樣做啦請賜教,謝謝。 compiler: devC++ 4.9.9.2 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.136.79.51 ※ 編輯: rockwalking 來自: 220.136.79.51 (06/19 01:16)

06/19 01:18, , 1F
雖說互相include不好 但我覺得你要解 先把定義拉到.cpp吧
06/19 01:18, 1F

06/19 01:19, , 2F
b.h的include在哪
06/19 01:19, 2F
※ 編輯: rockwalking 來自: 220.136.79.51 (06/19 01:21)

06/19 01:22, , 3F
回樓上以加上但錯誤訊息是一樣的
06/19 01:22, 3F

06/19 01:24, , 4F
1F建議我會考慮的
06/19 01:24, 4F

06/19 17:39, , 5F
class A; 那行不該存在吧
06/19 17:39, 5F

06/19 17:40, , 6F
沒事我看錯了 -.-
06/19 17:40, 6F
文章代碼(AID): #1C6wanRl (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1C6wanRl (C_and_CPP)