473,799 Members | 3,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Circular dependeny: Forward declaration does not help!

I have a circular dependency between two classes.
The FAQ hint about a forward declaration does not help in my case
([38.11] How can I create two classes that both know about each
other?)

Error: "field `foo' has incomplete type" for the following code:

#include <iostream>
#include <vector>

class FredVector; // forward declaration

class Fred {
public:
FredVector foo;
};

class FredVector : public std::vector<Fre d*>{
public:
~FredVector(){ // delete all Fred elements automagically
for (iterator p=begin(); p != end(); ++p)
delete *p;
}
};

If I turn it around and put FredVector before Fred and make a Fred
forward declaration, it results in:

test.cpp: In destructor `FredVector::~F redVector()':
test.cpp:9: Warnung: invalid use of undefined type `struct Fred'
test.cpp:4: Warnung: forward declaration of `struct Fred'

What can I do?

Thanks! Markus
Jul 22 '05 #1
6 2851

"Markus Dehmann" <ma*******@gmx. de> wrote in message
news:c1******** *************** ***@posting.goo gle.com...
I have a circular dependency between two classes.
The FAQ hint about a forward declaration does not help in my case
([38.11] How can I create two classes that both know about each
other?)

Error: "field `foo' has incomplete type" for the following code:

#include <iostream>
#include <vector>

class FredVector; // forward declaration
This will only allow you to define a pointer or
reference to type 'FredVector', until 'FredVector'
is completely defined.

class Fred {
public:
FredVector foo;
};

class FredVector : public std::vector<Fre d*>{
public:
~FredVector(){ // delete all Fred elements automagically
for (iterator p=begin(); p != end(); ++p)
delete *p;
}
};

If I turn it around and put FredVector before Fred and make a Fred
forward declaration, it results in:

test.cpp: In destructor `FredVector::~F redVector()':
test.cpp:9: Warnung: invalid use of undefined type `struct Fred'
test.cpp:4: Warnung: forward declaration of `struct Fred'
Same problem in the other direction.
What can I do?


You can define a pointer or reference member to the other type,
and initialize it somewhere after that type is completely defined.

-Mike
Jul 22 '05 #2
"Markus Dehmann" <ma*******@gmx. de> wrote in message news:c1******** *************** ***@posting.goo gle.com...
I have a circular dependency between two classes.
The FAQ hint about a forward declaration does not help in my case
It would if you were familiar with the concept of
complete and incomplete type definitions and
where they can be used.
([38.11] How can I create two classes that both know about each
other?)

Error: "field `foo' has incomplete type" for the following code:

#include <iostream>
#include <vector>

class FredVector; // forward declaration

class Fred {
public:
FredVector foo;
};

class FredVector : public std::vector<Fre d*>{
public:
~FredVector(){ // delete all Fred elements automagically
for (iterator p=begin(); p != end(); ++p)
delete *p;
}
};

If I turn it around and put FredVector before Fred and make a Fred
forward declaration, it results in:

test.cpp: In destructor `FredVector::~F redVector()':
test.cpp:9: Warnung: invalid use of undefined type `struct Fred'
test.cpp:4: Warnung: forward declaration of `struct Fred'

What can I do?
The following will do the job you were attempting:

class Fred; // forward declaration

class FredVector : public std::vector<Fre d*>{
public:
~FredVector();
};

class Fred {
public:
FredVector foo;
};

inline FredVector::~Fr edVector()
{ // delete all Fred elements automagically
for (iterator p=begin(); p != end(); ++p)
delete *p;
}

By the way, I do not mean to condone public derivation
from std::vector by posting this code. Why not is a
subject for another day.
Thanks! Markus

You're welcome.

--
--Larry Brasfield
email: do************* **********@hotm ail.com
Above views may belong only to me.
Jul 22 '05 #3
"Markus Dehmann" <ma*******@gmx. de> wrote...
I have a circular dependency between two classes.
The FAQ hint about a forward declaration does not help in my case
([38.11] How can I create two classes that both know about each
other?)

Error: "field `foo' has incomplete type" for the following code:

#include <iostream>
#include <vector>

class FredVector; // forward declaration

class Fred {
public:
FredVector foo;
};

class FredVector : public std::vector<Fre d*>{
public:
~FredVector(){ // delete all Fred elements automagically
for (iterator p=begin(); p != end(); ++p)
delete *p;
}
};

If I turn it around and put FredVector before Fred and make a Fred
forward declaration, it results in:

test.cpp: In destructor `FredVector::~F redVector()':
test.cpp:9: Warnung: invalid use of undefined type `struct Fred'
test.cpp:4: Warnung: forward declaration of `struct Fred'

What can I do?


#include <vector>

class Fred;

class FredVector : public std::vector<Fre d*> {
public:
~FredVector();
};

class Fred {
public:
FredVector foo;
};

FredVector::~Fr edVector() {
for (iterator p = begin(); p != end(); ++p)
delete *p;
}

int main () {
Fred f;
}

Should now compile fine.

There is a problem, however. You may end up having circular references
in your run-time data. Destruction of a Fred object causes clearing of
the 'foo' vector, which in turn causes destruction of other Fred objects
(through 'delete'). Make sure you never let two Fred objects store the
pointers to each other in their 'foo' vectors.

V
Jul 22 '05 #4
Larry Brasfield wrote:
"Markus Dehmann" <ma*******@gmx. de> wrote in message
news:c1******** *************** ***@posting.goo gle.com...
I have a circular dependency between two classes.
[...]
The following will do the job you were attempting:

class Fred; // forward declaration

class FredVector : public std::vector<Fre d*>{
public:
~FredVector();
};

class Fred {
public:
FredVector foo;
};

inline FredVector::~Fr edVector()
{ // delete all Fred elements automagically
for (iterator p=begin(); p != end(); ++p)
delete *p;
}

By the way, I do not mean to condone public derivation
from std::vector by posting this code. Why not is a
subject for another day.


Thanks for your help! But now you made me curious. Isn't this vector
subclass very nice? It automatically prevents memory leaks. I'm not allowed
to put autp_ptr objects into a vector. So, as an alternative, this solution
seemed viable for me.

Of course, I could use delegation instead of inheritance. But why shouldn't
I inherit from vector?

Thanks!
Markus
Jul 22 '05 #5
>> }

By the way, I do not mean to condone public derivation
from std::vector by posting this code. Why not is a
subject for another day.
Thanks for your help! But now you made me curious. Isn't this vector
subclass very nice? It automatically prevents memory leaks. I'm not
allowed
to put autp_ptr objects into a vector. So, as an alternative, this
solution
seemed viable for me.


You should use shared_ptr from boost, see www.boost.org.

Of course, I could use delegation instead of inheritance. But why
shouldn't
I inherit from vector?


One problem with you code is that FredVector objects don't copy or assign
correctly, but that is fixable.

Another problem is (as Victor pointed out) you have no protection again two
FredVector elements holding the same pointer. That is not so easy to fix.

A third problem is that you cannot write code like this

FredVector* f = new FredVector();
some_func(f);

void some_func(vecto r<Fred*>* v)
{
...
delete v;
}

This is because vector lacks a virtual destructor so deleting a derived
class through a vector pointer is undefined behaviour. This (minor) problem
is unsolvable.

All these problem however are avoided by using a vector of reference counted
smart pointers, such as the one I mentioned from boost.

John
Jul 22 '05 #6
ma*******@gmx.d e (Markus Dehmann) wrote in message news:<c1******* *************** ****@posting.go ogle.com>...
I have a circular dependency between two classes.
The FAQ hint about a forward declaration does not help in my case
([38.11] How can I create two classes that both know about each
other?)

Error: "field `foo' has incomplete type" for the following code:

#include <iostream>
#include <vector>

class FredVector; // forward declaration

class Fred {
public:
FredVector foo;
};

class FredVector : public std::vector<Fre d*>{
public:
~FredVector(){ // delete all Fred elements automagically
for (iterator p=begin(); p != end(); ++p)
delete *p;
}
};

If I turn it around and put FredVector before Fred and make a Fred
forward declaration, it results in:

test.cpp: In destructor `FredVector::~F redVector()':
test.cpp:9: Warnung: invalid use of undefined type `struct Fred'
test.cpp:4: Warnung: forward declaration of `struct Fred'

What can I do?

Thanks! Markus


#include <vector>

class Fred
{
typedef std::vector<Fre d*> FredVector;

public:
~Fred();

private:
FredVector fredVector;
};

inline Fred
::~Fred()
{
for(FredVector: :iterator p=fredVector.be gin();
p!=fredVector.e nd();
++p)
delete *p;
}

Non-private inheritance from std::vector is not a good idea because
std::vector's desctructor is not virtual.
Jul 22 '05 #7

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

Similar topics

11
3907
by: Jacek Dziedzic | last post by:
Hello! I'm creating a class that has a static method which would compute a lookup table needed later by all members of the class. The problem is, the lookup table is composed of records and one of the record fields is the class itself, i.e. I'm aiming at something like: typedef struct { my_class instance;
3
2051
by: crichmon | last post by:
Any general advice for dealing with circular dependencies? For example, I have a situation which, when simplified, is similar to: ///////////// // A.h class A { public: int x;
16
2845
by: Kiuhnm | last post by:
Is there an elegant way to deal with semi-circular definitions? Semi-circular definition: A { B }; B { *A }; Circular reference: A { *B }; B { *A }; The problems arise when there are more semi-circular definitions and
2
4769
by: blueblueblue2005 | last post by:
Hi, there was a post several days ago about using forward class declaration to solve the circular including issue, today, one question suddenly came into mind: which class should the forward class declaration statement be in? suppose I have a class A and B, sample code as following: // A.h #ifndef A_H #define A_H
3
2877
by: Rob Clark | last post by:
Hi, I have an issue which is the ordinary and always recurring circular dependency - but I do not know how to resolve it :-( The usual forward declaration does not help... I have a client that uses a resource. As a consequence of using the resource, the resource later receives an asynchronous notification (from an underlying layer, e.g. OS). This notification should then be forwarded to the Client that owns the resource.
3
6238
by: joshc | last post by:
I've found some old posts on this issue but the answer was a bit vague to me. The problem I am trying to solve is the following: /***** a.h ***********/ #include "b.h" /* Forward declaration */ struct type2; typedef struct type1
5
2959
by: fomas87 | last post by:
Hi guys, I'm writing a framework for an sdl gui in c++, but got stuck on the circular dependency, I've read a lot of articles about resolving dependencies and about forward declaration but none of them seem to work for me. Basically I've got the Component abstract class which is the base class for all elements like Buttons, Labels, CheckBoxes, Containers and so on. And every Component has a parent of type Container. But here is the thing,...
6
3474
by: Mosfet | last post by:
Hi, I have two classes, let's call them class A and class B with mutual dependencies as shown below and where implementation is inside .h (no cpp) #include "classB.h" class A {
2
1340
by: Armin Zingler | last post by:
Hi, I'm using VC++ 2008 Express. I have a managed interface. One of it's member uses a type that is declared later in the same file. The type implements the interface declared before. So, I have circular relations between the two declarations. Question: How do I have to forward-declare the struct? (C++ is still not clever enough to see the declaration below.)
0
9688
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, 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...
0
10491
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, 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...
0
10268
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10031
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6809
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();...
0
5467
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...
0
5593
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4146
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
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.