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 includes are cyclic. Since
I'm using
#ifndef _FILENAME
#define _FILENAME
....
#endif
in the header files, the include of header file
of class A in class C is not performed.
Is there any way to solve that problem or should I revise
the class design?
Chris 8 2880
Christian Christmann wrote: 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 includes are cyclic. Since I'm using
#ifndef _FILENAME #define _FILENAME ... #endif
in the header files, the include of header file of class A in class C is not performed.
Is there any way to solve that problem or should I revise the class design?
Forward declarations usually solve this problem. By declaring, for
example, class B in the header of class A, you don't need to include
the header.
You could perhaps split the headers in different parts. For example, if
the header for class B contains the class definition and something
else, you could put the something else elsewhere. Perhaps then class C
would be able to include that something else from elsewhere.
But if you really need to include them that way, well, C++ can't do
nothing for you.
Jonathan
> Forward declarations usually solve this problem. By declaring, for example, class B in the header of class A, you don't need to include the header.
I've defined class B in class A.
But then the compiler issues an error message:
field `XXX' has incomplete type
[where XXX is an member attribute of type B defined in class A] You could perhaps split the headers in different parts. For example, if the header for class B contains the class definition and something else, you could put the something else elsewhere. Perhaps then class C would be able to include that something else from elsewhere.
That's unfortunately not possible since I'm not the author of one of the
huge header files which could possibly be split.
Jonathan
-Chris
Christian Christmann wrote: Forward declarations usually solve this problem. By declaring, for example, class B in the header of class A, you don't need to include the header.
I've defined class B in class A. But then the compiler issues an error message:
field `XXX' has incomplete type
[where XXX is an member attribute of type B defined in class A]
Forward declare C in B and A in C.
Christian Christmann wrote: Forward declarations usually solve this problem. By declaring, for example, class B in the header of class A, you don't need to include the header.
I've defined class B in class A. But then the compiler issues an error message:
field `XXX' has incomplete type
[where XXX is an member attribute of type B defined in class A]
With a forward declaration, you can only refer to the name, not to its
member or its size.
class A;
int main()
{
A *a = 0; // ok
a = new A; // error: size
a->doit(); // error: member
delete a; // error: size
}
Forward declare those you can. For example, if you only have a pointer,
a reference or are only using the name in parameter lists and return
types, you can declare the name. For everything else (inheritance,
objects on the stack, operations requiring size or members), you need
the class definition. You could perhaps split the headers in different parts. For example, if the header for class B contains the class definition and something else, you could put the something else elsewhere. Perhaps then class C would be able to include that something else from elsewhere.
That's unfortunately not possible since I'm not the author of one of the huge header files which could possibly be split.
Well it looks like you're stuck.
Jonathan
> Forward declare C in B and A in C.
And how should the #include <...h> look like?
Thank you
Chris
If you have something like:
class A; // forward declaration
class B
{
A a_;
};
this will generate compiler error, since type B is incomplete (it
depends on type A, which is not defined).
You can solve it by keeping a reference or a pointer to A:
class B
{
A* pa_;
};
You can also rething your design: try to remove cyclic dependencies
between classes, if possible.
Christian Christmann wrote: Forward declare C in B and A in C.
And how should the #include <...h> look like?
'Forward declaration' means that you forward declare (but _not_
#include) in header files and #include in source (*.cpp) files. In
practice you can forward declare in headers everything except types for
data members.
> 'Forward declaration' means that you forward declare (but _not_ #include) in header files and #include in source (*.cpp) files. In practice you can forward declare in headers everything except types for data members.
Thank you very much.
-Chris This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Thomas Mailund |
last post by:
Hi group.
I have a problem with some C extensions I am working with and
hope that some of you can help. Basically, I am wrapping a a tree
structure from C where I have python methods for extracting either the
entire tree or subtrees; since I don't want the full tree to be
deallocated while python has references to the subtrees, I INCREF the
full tree whenever I hand out a subtree reference. I don't want any of
the subtrees to be...
|
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 project 1-solution A
accessing a "save" method in project 2-solution B as well as "getinfo"
method in project 2 accessing a "read" method in project A.
Is this permitted? I am getting the "dependency file in project cannot be
copied... conflict...
|
by: John Doe |
last post by:
Hi all,
Regarding those cyclic dependencies of classes (like cases in which
class A refers to class B and class B to class A): can they ALL be
resolved by forward defining classes, and by implementing the actual
functions outside the class definition, OR there are STILL cases which
cannot be solved in this way and could require rewriting your code /
rewriting your class hierarchy?
|
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 to
compile .....
I have two classes each having their own header files, including each
other. A forward decleration doesn't seem to be enough because they
also call function calls within the classes. How do I solve this ???
|
by: Matthias Kramm |
last post by:
Hi All,
I'm having a little bit of trouble using the "imp" module to
dynamically import modules. It seems that somehow cyclic references of
modules don't work.
I'm unable to get the following to work:
I've got the following files:
web/__init__.py
| |
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 inclusion
relationships.
Another question I have is how to solve this dependency when "typedef"
is used. usually we can use forward declaration like "struc A; " to
solve references like "struct A* p". But in many places we have
|
by: toton |
last post by:
Hi I have a class called Session, which stores a vector of Page, like
vector<PageAlso each Page need's to know the Session to which it is
attached.
Thus I have, (the classes has many other things, I copied only the
portion i needed)
in Session.hpp
#include "Page.hpp"
class Session {
|
by: Joe Peterson |
last post by:
I've been doing a lot of searching on the topic of one of Python's more
disturbing issues (at least to me): the fact that if a __del__ finalizer
is defined and a cyclic (circular) reference is made, the garbage
collector cannot clean it up.
First of all, it seems that it's best to avoid using __del__. So far, I
have never used it in my Python programming. So I am safe there. Or am
I? Also, to my knowledge, I have never created a...
|
by: pallav |
last post by:
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
{
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |