CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Egg or chicken?

  1. #1
    Join Date
    Nov 2001
    Location
    Mendoza, Argentina
    Posts
    75

    Egg or chicken?

    Hello y'all!

    Here's a tricky situation. Probably it's quite silly, but I just don't see it (and it's giving me quite a headache)

    let's say we have two classes: ClassA and ClassB. We need both classes to interact with eachother so in their private atributes we should find pointers to eachother. The headers should look somehting like...

    classa.h

    #ifndef __classa_h_
    #define __classa_h_

    #include "classb.h"

    class ClassA {
    public:
    /* public members here*/
    private:
    ClassB * ptr_cb;
    };

    #endif

    and...

    classb.h

    #ifndef __classb_h_
    #define __classb_h_

    #include "classa.h"

    class ClassB {
    public:
    /* public members here*/
    private:
    ClassA * ptr_ca;
    };

    #endif


    the trick here is that, no matter where the compiler starts, either one or another ".h" is not reached, so it tells me that either "ClassA" or "ClassB" is not defined.

    I know what's going on, but I don't know how to solve it. I can't see a way to make both classes definition see eachother.

    Any Ideas?

    Steempee

  2. #2
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    Use forward references instead of including the files.

    Use

    class ClassA;

    instead of

    #include "classa.h"

    Similarly for ClassB.

    Then in the implementation file, include what you need. This is what you want to do in general, as it reduces include dependencies. Only include what is absolutely necessary in header files. This becomes more important the larger your project is. The last thing you want to do is start a 4 hour compile after changing a single header file.

    Jeff

  3. #3
    Join Date
    Nov 2001
    Location
    Mendoza, Argentina
    Posts
    75

    Talking Yay!!

    Thank you Jeff!! It works perfectly!!

    You've saved me a bunch of money in aspirins :P

    Thanks again!

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured