How to include Header file in....
-
Hello, I have a global header file called TypeDefs.H, this file is included in header file of every class of my project. because all classes use custom types. Now I have a class called CMyData which is also a member of some structs in TypeDefs.H, so I have to include MyData.H in TypeDefs.H problem is some members of CMyData are custom and defined in TypeDefs.H so I also have to include TypeDefs.H in MyData.H I agree that it may seem ugly but in this way I can keep things organized in my brain, but compiler dose not like that and shouts loudly with thousands of error messages. I even tried to use
#ifndef MY_DATA_H_ #define MY_DATA_H_ ... #endif
in MyData.H and#ifndef TYPE_DEFS_H_ #define TYPE_DEFS_H_ ... #endif
in TypeDefs.H but problem persists can someone kindly tell me how can I get over this problem:) -
Hello, I have a global header file called TypeDefs.H, this file is included in header file of every class of my project. because all classes use custom types. Now I have a class called CMyData which is also a member of some structs in TypeDefs.H, so I have to include MyData.H in TypeDefs.H problem is some members of CMyData are custom and defined in TypeDefs.H so I also have to include TypeDefs.H in MyData.H I agree that it may seem ugly but in this way I can keep things organized in my brain, but compiler dose not like that and shouts loudly with thousands of error messages. I even tried to use
#ifndef MY_DATA_H_ #define MY_DATA_H_ ... #endif
in MyData.H and#ifndef TYPE_DEFS_H_ #define TYPE_DEFS_H_ ... #endif
in TypeDefs.H but problem persists can someone kindly tell me how can I get over this problem:)I suggest that you use a combination of 2 approaches depending on the types, classes or non class types, you're defining. 1. A third header file e.g. BaseTypes.h which contains the definitions out of MyData.h and TypeDefs.h that don't depend on anything else. You can include this file from both MyData.h and TypeDefs.h and this should remove some of your problems. If you still find both header files are dependent on one another you might want to actaully draw out a dependency tree to work out how to further divide your files. 2. Use forward delclaration to introduce names to the compiler before you define them. Remember that you can only use the forward declared name in places where the size of what it refers to is not an issue for the compiler like declaring a pointer.
//Forward declaration class CMyAwkwardClass; //Declare a class that uses CMyAwkward even though we don't know what it is yet class CSomeOtherClass { private: CMyAwkwardClass* m_pAwkward; };
Careful use of this sort of thing may get you out of a hole. :)Nothing is exactly what it seems but everything with seems can be unpicked.
-
I suggest that you use a combination of 2 approaches depending on the types, classes or non class types, you're defining. 1. A third header file e.g. BaseTypes.h which contains the definitions out of MyData.h and TypeDefs.h that don't depend on anything else. You can include this file from both MyData.h and TypeDefs.h and this should remove some of your problems. If you still find both header files are dependent on one another you might want to actaully draw out a dependency tree to work out how to further divide your files. 2. Use forward delclaration to introduce names to the compiler before you define them. Remember that you can only use the forward declared name in places where the size of what it refers to is not an issue for the compiler like declaring a pointer.
//Forward declaration class CMyAwkwardClass; //Declare a class that uses CMyAwkward even though we don't know what it is yet class CSomeOtherClass { private: CMyAwkwardClass* m_pAwkward; };
Careful use of this sort of thing may get you out of a hole. :)Nothing is exactly what it seems but everything with seems can be unpicked.
Thanks Matthew, Do I have to destroy CMyAwkwardClass* m_pAwkward manually when exiting the program to prevent memory leakage? Thanks
-
Thanks Matthew, Do I have to destroy CMyAwkwardClass* m_pAwkward manually when exiting the program to prevent memory leakage? Thanks
Electronic75 wrote:
CMyAwkwardClass* m_pAwkward manually when exiting the program to prevent memory leakage
Only if you initialize it using the
new
operator or a copy constructor or ..., not if it is only used as a pointer.http://www.readytogiveup.com/[^] - Do something special today. http://www.totalcoaching.ca/[^] - Give me some feedback about this site !
-
Thanks Matthew, Do I have to destroy CMyAwkwardClass* m_pAwkward manually when exiting the program to prevent memory leakage? Thanks
Memory management is a whole other issue. When exiting the progrma Windows throws everything away anyway. Still when you've finished using anything m_pAwkward points at you should delete it. Every
m_pAwkward = new CMyAwkwardClass();
needs a matchingdelete m_pAwkward;
. Remember that means matching in terms of the calls actually made at runtime not just the text of the source code. Enjoy.:)Nothing is exactly what it seems but everything with seems can be unpicked.
-
Electronic75 wrote:
CMyAwkwardClass* m_pAwkward manually when exiting the program to prevent memory leakage
Only if you initialize it using the
new
operator or a copy constructor or ..., not if it is only used as a pointer.http://www.readytogiveup.com/[^] - Do something special today. http://www.totalcoaching.ca/[^] - Give me some feedback about this site !
Rage wrote:
Only if you initialize it using the new operator or a copy constructor or ..., not if it is only used as a pointer.
Hey Rage, that's the pointers! it needs to be initialized with "new", otherwise they point to nowhere and so need the "delete" then. Rather you meant the variables that get allocated on stack? like CmyClass myCls; Just got the context. -- modified at 14:40 Thursday 19th July, 2007