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. Circular reference in header file

Circular reference in header file

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
5 Posts 2 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.
  • M Offline
    M Offline
    Mr Freeze
    wrote on last edited by
    #1

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

    T 1 Reply Last reply
    0
    • M Mr Freeze

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

      T Offline
      T Offline
      Tim Smith
      wrote on last edited by
      #2

      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.

      M 1 Reply Last reply
      0
      • T Tim Smith

        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.

        M Offline
        M Offline
        Mr Freeze
        wrote on last edited by
        #3

        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??

        T 1 Reply Last reply
        0
        • M Mr Freeze

          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??

          T Offline
          T Offline
          Tim Smith
          wrote on last edited by
          #4

          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.

          M 1 Reply Last reply
          0
          • T Tim Smith

            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.

            M Offline
            M Offline
            Mr Freeze
            wrote on last edited by
            #5

            It's working now, thanks :) Best regards

            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