#include headers that include this header

Discussion in 'C++' started by Aguilar, James, Jul 16, 2004.

  1. Suppose I have classes Foo and Bar. They depend on each other, but other
    classes also depend on them. Moreover, their functionality is distinct.
    How can I include both of them?

    Specifically, Foo is a class that holds pointers to a variety of objects, as
    in:

    class Foo
    {
    public: ...
    private:
    Bar a
    Baz b
    FooBar c
    }

    So, I include the headers for Bar, Baz, and FooBar like so:

    //Foo.hh
    #ifndef _FOO_HH_
    #define _FOO_HH_ 1
    #include "Bar.hh"
    #include "Baz.hh"
    #include "FooBar.hh"
    .... //class defn.
    #endif

    But the class bar needs a pointer to the Foo object that calls its
    constructor. So:

    //Bar.hh
    #ifndef _BAR_HH_
    #define _BAR_HH_ 1
    #include "Foo.hh"
    .... //class defn.
    #endif

    However, if I'm compiling Bar.hh, it doesn't work, because when it Foo.hh
    tries to include Bar.hh, it finds that _BAR_HH_ is defined. What is the
    solution to this problem? I was thinking of a .hh files with declarations
    of all the various classes that I will define at any point and putting that
    at the top of every header file. Is that good?
    Aguilar, James, Jul 16, 2004
    #1
    1. Advertising

  2. Aguilar, James wrote:
    > Suppose I have classes Foo and Bar. They depend on each other, [...]


    Read about "forward declaration" in the FAQ.
    Victor Bazarov, Jul 16, 2004
    #2
    1. Advertising

  3. "Victor Bazarov" <> wrote in message
    news:VxUJc.2095$...
    > Aguilar, James wrote:
    > > Suppose I have classes Foo and Bar. They depend on each other, [...]

    >
    > Read about "forward declaration" in the FAQ.


    Many thanks. This solves my problem.
    Aguilar, James, Jul 16, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dont bother
    Replies:
    0
    Views:
    759
    dont bother
    Mar 3, 2004
  2. Andreas Bogenberger
    Replies:
    3
    Views:
    858
    Andreas Bogenberger
    Feb 22, 2008
  3. mlt
    Replies:
    2
    Views:
    804
    Jean-Marc Bourguet
    Jan 31, 2009
  4. Phil
    Replies:
    4
    Views:
    654
    Gabriel Genellina
    Jan 17, 2010
  5. Ian
    Replies:
    2
    Views:
    1,901
Loading...

Share This Page