CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2003
    Durham, England

    cross-referencing #includes

    This may sound a n00b question, but say I have two files, a.h and b.h. a.h contains a class, a, and b.h contains a class,b. class a has a member of type b, and vice versa for b. How would i arrange the include statements so that it compiles correctly?? thankyou.

  2. #2
    Join Date
    Jul 2003
    Include both

  3. #3
    Join Date
    Aug 2002
    You can't do this directly. One solution is to use a third header file called defs.h which has forward declarations for the classes. However that presupposes that you are using pointers to classes.

    It *can* also be an indication that your design is bad. In any case, I would look at the design again and ask myself, why I would need to do this.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  4. #4
    Join Date
    Jan 2004
    Put a forward declaration in each file, eg

    In a.h
    class b; // forward declaration
    //note do NOT #include "b.h"

    class a
    b m_b;
    In b.h
    #include "a.h"

    class b
    a m_a;
    In the classes, stick to using pointers, otherwise you may find you run into problems, depending on what you're trying to do with the other class.

    You may need to do the forward declaration the other way around, depending on which file your compiler looks at first.

    This may be a sign of bad design, but is frequently inevitable. For example you have to do this to implement the 'visitor pattern' - a famous OO design solution.
    Last edited by JonnoA; February 6th, 2004 at 02:11 PM.

  5. #5
    Join Date
    May 2000
    KY, USA
    Take a look at the following FAQ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.