Circular dependeny: Forward declaration does not help!

M

Markus Dehmann

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
 
M

Mike Wahler

Markus Dehmann said:
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
 
L

Larry Brasfield

Markus Dehmann said:
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.
 
V

Victor Bazarov

Markus Dehmann said:
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
 
M

Markus Dehmann

Larry said:
Markus Dehmann said:
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
 
J

John Harrison

}
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
 
A

Andre Dajd

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top