Thursday, July 9, 2009

C++ mutual-inclusion (mutual dependcies)?

Hi all. I am having an issue using Borland C++ Builder where I have 2 classes:


TTarget


TTgtMgr





Both classes include references to the other in the defintions (header) and the implementation (cpp)





I tried using forward declarations. This allows me to make vars


TTarget *tgt


TTgtMgr *mgr





in each others classes, however the problem is, in the implementation, I try to access the methods of the other class and I recieve the following compiler error:





Cannot access methods of %26lt;class%26gt; because %26lt;class%26gt; is not yet defined.





So for example in TTarget.cpp I do:


*TTgtMgr *mgr


mgr-%26gt;someMgrFunc();





And the error occurs.





I feel like some form of late binding might help but I really dont understand it well enough to implement such a fix.





Any thoughts on how I can remedy this problem?





Thanks!

C++ mutual-inclusion (mutual dependcies)?
In the .h files of the above two classes use forward declaration, no "*" required.





In TTarget.h before actual class definitions starts








/////////// TTarget.h file





class TTgtMgr; //forward declaration





class TTarget{





}








/////////// TTgtMgr.h file





class TTarget; //forward declaration





class TTgtMgr{





}





fill in the rest of the implementations


No comments:

Post a Comment