Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to include Header file in....

How to include Header file in....

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Electronic75
    wrote on last edited by
    #1

    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:)

    M 1 Reply Last reply
    0
    • E Electronic75

      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:)

      M Offline
      M Offline
      Matthew Faithfull
      wrote on last edited by
      #2

      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.

      E 1 Reply Last reply
      0
      • M Matthew Faithfull

        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.

        E Offline
        E Offline
        Electronic75
        wrote on last edited by
        #3

        Thanks Matthew, Do I have to destroy CMyAwkwardClass* m_pAwkward manually when exiting the program to prevent memory leakage? Thanks

        R M 2 Replies Last reply
        0
        • E Electronic75

          Thanks Matthew, Do I have to destroy CMyAwkwardClass* m_pAwkward manually when exiting the program to prevent memory leakage? Thanks

          R Offline
          R Offline
          Rage
          wrote on last edited by
          #4

          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 !

          E 1 Reply Last reply
          0
          • E Electronic75

            Thanks Matthew, Do I have to destroy CMyAwkwardClass* m_pAwkward manually when exiting the program to prevent memory leakage? Thanks

            M Offline
            M Offline
            Matthew Faithfull
            wrote on last edited by
            #5

            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 matching delete 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.

            1 Reply Last reply
            0
            • R Rage

              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 !

              E Offline
              E Offline
              Eytukan
              wrote on last edited by
              #6

              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


              Best wishes to Rexx[^]

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups