Cyclic header dependency

Discussion in 'C++' started by pallav, Apr 12, 2007.

  1. pallav

    pallav Guest

    I have to header files, circuit.h and latch.h that reference each
    other and are causing a cyclic dependency.

    latch.h file

    #include "circuit.h"
    typedef boost::shared_ptr<struct Latch> LatchPtr;

    class Latch
    {
    ...
    CircuitPtr circuit;
    };

    circuit.h file

    #include "latch.h"
    typedef boost::shared_ptr<struct Circuit> CircuitPtr;

    class Circuit
    {
    ...
    list<LatchPtr> latches;
    };

    As you can see there is a cyclic dependency. To get things to compile,
    what I've done is
    I've changed the circuit.h file as follows:

    new circuit.h file

    // defined in latch.h, can't do #include "latch.h" as that will
    introduce cyclic dependency
    extern typedef boost::shared_ptr<struct Latch> LatchPtr;

    typedef boost::shared_ptr<struct Circuit> CircuitPtr;

    class Circuit
    {
    ...
    list<LatchPtr> latches;
    };

    This solves the problem, but I'm wondering if this is the way to fix
    such issues. I know C++ has forward declarations but I"m still trying
    to figure out how to use them and if they'll solve this. The other
    option is to combine the two header files but I would prefer not to do
    that.

    Is the above fix bad practice? thanks for your time.
    pallav, Apr 12, 2007
    #1
    1. Advertising

  2. pallav

    pallav Guest

    an update on this problem. i was able to use forward declarations to
    fix this but it seems like Latch has to make a call to circuit object
    member functions in one of its members:

    void Latch::setControl(const NodePtr& n)
    {
    if (!n)
    {
    CircuitPtr cir = n->getCircuit();
    if (!cir)
    bailOut("Latch::setControl()", ERROR_INVALID_CIRCUIT);
    // TODO: uncomment once circuit has latch support.
    cir->addLatch(n, this->shared_from_this());
    }
    control = n;
    }

    so i will need #include "circuit.h" so forward declarations. Similarly
    function members in Circuit:: need to call members of Latch:: so i
    guess i have to combine the header files? No other way?
    thanks
    pallav, Apr 12, 2007
    #2
    1. Advertising

  3. pallav

    Guest

    On Apr 12, 4:45 pm, "pallav" <> wrote:
    > an update on this problem. i was able to use forward declarations to
    > fix this but it seems like Latch has to make a call to circuit object
    > member functions in one of its members:
    >
    > void Latch::setControl(const NodePtr& n)
    > {
    > if (!n)
    > {
    > CircuitPtr cir = n->getCircuit();
    > if (!cir)
    > bailOut("Latch::setControl()", ERROR_INVALID_CIRCUIT);
    > // TODO: uncomment once circuit has latch support.
    > cir->addLatch(n, this->shared_from_this());
    > }
    > control = n;
    >
    > }
    >
    > so i will need #include "circuit.h" so forward declarations. Similarly
    > function members in Circuit:: need to call members of Latch:: so i
    > guess i have to combine the header files? No other way?
    > thanks




    I didn't go through the detail of your code. However, I think what you
    need in the header file is only a pointer, which does not make it
    necessary to include the header file to introduce the name. You can
    use forward declaration in header file and include the header file in
    the source file.
    , Apr 12, 2007
    #3
  4. pallav

    Michael Guest

    On Apr 12, 1:45 pm, "pallav" <> wrote:
    > an update on this problem. i was able to use forward declarations to
    > fix this but it seems like Latch has to make a call to circuit object
    > member functions in one of its members:
    >
    > void Latch::setControl(const NodePtr& n)
    > {
    > if (!n)
    > {
    > CircuitPtr cir = n->getCircuit();
    > if (!cir)
    > bailOut("Latch::setControl()", ERROR_INVALID_CIRCUIT);
    > // TODO: uncomment once circuit has latch support.
    > cir->addLatch(n, this->shared_from_this());
    > }
    > control = n;
    >
    > }
    >
    > so i will need #include "circuit.h" so forward declarations. Similarly
    > function members in Circuit:: need to call members of Latch:: so i
    > guess i have to combine the header files? No other way?
    > thanks


    Your .cc or .cpp file will need to #include both for this to work.
    But that doesn't mean you need to combine them or that one .h file
    needs to #include the other.
    Michael, Apr 13, 2007
    #4
    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. Dennis Lerche

    Cyclic Dependency problem

    Dennis Lerche, May 11, 2004, in forum: C++
    Replies:
    6
    Views:
    11,206
    Khaled.Jouda
    Jul 5, 2010
  2. Replies:
    3
    Views:
    628
    Michael Mair
    Apr 22, 2006
  3. pallav

    Cyclic header dependency

    pallav, Apr 12, 2007, in forum: C++
    Replies:
    1
    Views:
    504
  4. pallav

    Cyclic header dependency

    pallav, Apr 12, 2007, in forum: C++
    Replies:
    0
    Views:
    370
    pallav
    Apr 12, 2007
  5. pallav

    Cyclic header dependency

    pallav, Apr 12, 2007, in forum: C++
    Replies:
    1
    Views:
    583
Loading...

Share This Page