Circular reference in header file
-
Hi, Imagine you have two classes with the following headers: //***************** Class CFirstClass **************** #include "SecondClass.h" class CFirstClass { public: CFirstClass(); virtual ~CFirstClass(); CSecondClass* pointer; }; //***************** Class CSecondClass **************** #include "FirstClass.h" class CSecondClass { public: CSecondClass(); virtual ~CSecondClass(); CFirstClass* pointer; }; We have a situation of a circular reference and I don't know to handle that problem. I tried with a forwarding instead of '#include "SecondClass.h"', but I get the error: 'use of undefined type 'CSecondClass'' !!! My concret problem is much more complex but I think I can summarize it with the above description. Does someone know how to solve this easily? (without building the second class into the first one) Thanks for your help :)
-
Hi, Imagine you have two classes with the following headers: //***************** Class CFirstClass **************** #include "SecondClass.h" class CFirstClass { public: CFirstClass(); virtual ~CFirstClass(); CSecondClass* pointer; }; //***************** Class CSecondClass **************** #include "FirstClass.h" class CSecondClass { public: CSecondClass(); virtual ~CSecondClass(); CFirstClass* pointer; }; We have a situation of a circular reference and I don't know to handle that problem. I tried with a forwarding instead of '#include "SecondClass.h"', but I get the error: 'use of undefined type 'CSecondClass'' !!! My concret problem is much more complex but I think I can summarize it with the above description. Does someone know how to solve this easily? (without building the second class into the first one) Thanks for your help :)
Ok, the simple solution is that you can add.
class CSecondClass;
To the start of FirstClass.h. This will at least allow you to specify pointers or references to the class in CFirstClass. However, you won't actually be able to do any operations on them. If you have to do operations on the classes, then you will have to remove the inline routines from the class definition. Tim Smith Descartes Systems Sciences, Inc.
-
Ok, the simple solution is that you can add.
class CSecondClass;
To the start of FirstClass.h. This will at least allow you to specify pointers or references to the class in CFirstClass. However, you won't actually be able to do any operations on them. If you have to do operations on the classes, then you will have to remove the inline routines from the class definition. Tim Smith Descartes Systems Sciences, Inc.
Thanks Tim, I already tried the forwarding with 'class CSecondClass', but since I need to do some operations on the pointers, I get the error 'use of undefined type 'CSecondClass''. I don't use the keyword 'inline' at all and I don't put any function body in the header file. What is wrong??
-
Thanks Tim, I already tried the forwarding with 'class CSecondClass', but since I need to do some operations on the pointers, I get the error 'use of undefined type 'CSecondClass''. I don't use the keyword 'inline' at all and I don't put any function body in the header file. What is wrong??
Even though the compiler doesn't have to generate code when it parses that inline function, it still has to have the class definied in order to parse the function. I have always had to move the offending inline functions out of the class definition and place them in the CPP file or in an .inl file that is included after both class definitions have been included. Tim Smith Descartes Systems Sciences, Inc.
-
Even though the compiler doesn't have to generate code when it parses that inline function, it still has to have the class definied in order to parse the function. I have always had to move the offending inline functions out of the class definition and place them in the CPP file or in an .inl file that is included after both class definitions have been included. Tim Smith Descartes Systems Sciences, Inc.