#include headers that include this header

A

Aguilar, James

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top