473,322 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Cyclic header dependency

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 LatchLatchPtr;

class Latch
{
...
CircuitPtr circuit;
};

circuit.h file

#include "latch.h"
typedef boost::shared_ptr<struct CircuitCircuitPtr;

class Circuit
{
...
list<LatchPtrlatches;
};

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 LatchLatchPtr;

typedef boost::shared_ptr<struct CircuitCircuitPtr;

class Circuit
{
...
list<LatchPtrlatches;
};

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.

Apr 12 '07 #1
3 4921
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

Apr 12 '07 #2
On Apr 12, 4:45 pm, "pallav" <pallavgu...@gmail.comwrote:
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 '07 #3
On Apr 12, 1:45 pm, "pallav" <pallavgu...@gmail.comwrote:
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.

Apr 13 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Brian Sabolik | last post by:
I'm not sure if I've broken any Object Oriented rules or not, but ... I have projects in 2 different solutions that need to use each other's methods. Therefore I may have an "update" method in...
3
by: Dennis Lerche | last post by:
Hi I have a problem regarding cyclic dependency, yeahh I know bad design. But right at this moment I can't see how it should be redesigned to avoid this. The problem is that I just can't get it...
8
by: Christian Christmann | last post by:
Hi, I've a problem with including header files. class A requires header file of class B class B requires header file of class C class C requires header file of class A As can be seen the...
4
by: Alex Sedow | last post by:
For example, System.dll contains assembly reference to System.XML.dll, System.XML.dll refers to System.dll. Some another open projects contain cyclic assembly dependencies too. How C# compiler...
3
by: fc2004 | last post by:
Hi, Is there any tools that could report where cyclic header dependency happens? this would be useful when working with a large project where tens or hundreds of headers files may form complex...
4
by: sakis.panou | last post by:
Hi all, Can anyone explain to me why the copy constructor of the COuterClass is getting called for this one? Let me start by saying I reckon this is seriously bad way of implementing anything of...
1
by: pallav | last post by:
I have two 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 LatchLatchPtr;...
3
by: soup007 | last post by:
Hi, I am having some difficulties with cyclic dependency between two classes. Situation is something like following - ///A.h #include "B.h" class A { { int X; public:
6
by: scruggsy | last post by:
Hi, got a possibly stupid question. I have 2 header files each containing class definitions along the lines of: // Header1.h #pragma once #include "Header2.h" // for Class2 class ClassA { ...
11
by: sgurukrupagmailcom | last post by:
Hi, When searching for a solution to my problem I stumbled upon 'Curiously Recurring Template Pattern' see link. This is how the pattern looks: template < typename T > struct y { } ;
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.