Sign in to follow this  
VarnishedOtter

Circular Dependency Problem

Recommended Posts

Hi,

I am having a problem with circular dependency and forward declaration doesn't seem to solve it.

Here is the situation:


class A contains an object of class B
class B contains a std::vector of class A objects

class B is declared first with a forward declaration
class A is defined
class B is defined

I am getting an error that class B is undefined when I use it in class A.

Thanks for any help.

Regards,
-Matt

Share this post


Link to post
Share on other sites
You will need to use pointers and forward declarations for this to work.
[code]
//In B.h
struct A;

struct B {
std::vector<A*> theAs;
};

//In A.h
struct B;

struct A {
B* parent;
};

#include <vector>
#include "A.h"
#include "B.h"

int main() {
B b;
A* a = new A();
a.parent = &b;
b.theAs.push_back(a);

for(size_t i = 0; i < b.theAs.size(); ++i)
delete b.theAs[i]; //what you new, you delete
}
[/code]

Additionally, you'll need to new the A's and B's (depending on various factors), you should use smart pointers, and if you don't you have to delete the pointers to everything you new...

Share this post


Link to post
Share on other sites
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]Hi Washu,[/left][/size][/font][/color]

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]Thanks for the solution, it works. :)[/left][/size][/font][/color]


[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]Regards,[/left][/size][/font][/color]
[left]-Matt[/left]

Share this post


Link to post
Share on other sites
[quote name='Washu' timestamp='1329793414' post='4915049']
You will need to use pointers and forward declarations for this to work.
[code]
//In B.h
struct A;

struct B {
std::vector<A*> theAs;
};

//In A.h
struct B;

struct A {
B* parent;
};

#include <vector>
#include "A.h"
#include "B.h"

int main() {
B b;
A* a = new A();
a.parent = &b;
b.theAs.push_back(a);

for(size_t i = 0; i < b.theAs.size(); ++i)
delete b.theAs[i]; //what you new, you delete
}
[/code]

Additionally, you'll need to new the A's and B's (depending on various factors), you should use smart pointers, and if you don't you have to delete the pointers to everything you new...
[/quote]

It's not just pointers, you can do this with references as well:
[code]
class SomeClass;

class MyClass
{
public:
MyClass(SomeClass &someOtherClass);

private:
SomeClass &mSomeOtherClass;
};
[/code]

Share this post


Link to post
Share on other sites
You generally do not want to store references as members (because you cannot reseat references). I believe it is illegal to try to store references in a standard container.

So while yes, sometimes you can use references in circular situations, I don't think it would be appropriate here.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this