473,386 Members | 1,702 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,386 software developers and data experts.

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<Fred*>{
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::~FredVector()':
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 2820

"Markus Dehmann" <ma*******@gmx.de> wrote in message
news:c1**************************@posting.google.c om...
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<Fred*>{
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::~FredVector()':
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.google.c om...
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<Fred*>{
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::~FredVector()':
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<Fred*>{
public:
~FredVector();
};

class Fred {
public:
FredVector foo;
};

inline FredVector::~FredVector()
{ // 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***********************@hotmail.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<Fred*>{
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::~FredVector()':
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<Fred*> {
public:
~FredVector();
};

class Fred {
public:
FredVector foo;
};

FredVector::~FredVector() {
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.google.c om...
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<Fred*>{
public:
~FredVector();
};

class Fred {
public:
FredVector foo;
};

inline FredVector::~FredVector()
{ // 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(vector<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.de (Markus Dehmann) wrote in message news:<c1**************************@posting.google. 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<Fred*>{
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::~FredVector()':
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<Fred*> FredVector;

public:
~Fred();

private:
FredVector fredVector;
};

inline Fred
::~Fred()
{
for(FredVector::iterator p=fredVector.begin();
p!=fredVector.end();
++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
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...
3
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
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...
2
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...
3
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...
3
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...
5
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...
6
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
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...
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:
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...
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
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...

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.