473,395 Members | 1,658 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,395 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 4928
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.