473,387 Members | 1,693 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,387 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 4927
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.