Jump to content
  • Advertisement
Sign in to follow this  
VarnishedOtter

Circular Dependency Problem

This topic is 2338 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
You will need to use pointers and forward declarations for this to work.

//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; //what you new, you delete
}


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]

Hi Washu,

[/font]

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

Thanks for the solution, it works. :)

[/font]


[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

Regards,

[/font]

-Matt

Share this post


Link to post
Share on other sites

You will need to use pointers and forward declarations for this to work.

//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; //what you new, you delete
}


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...


It's not just pointers, you can do this with references as well:

class SomeClass;

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

private:
SomeClass &mSomeOtherClass;
};

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!