Jump to content
  • Advertisement
Sign in to follow this  
Red Ant

[C++] Cyclic inclusion hell

This topic is 4801 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

Hello, I'm stuck. I have a class A and a type std::list< A >, which obviously stores instances of class A. This in itself doesn't pose any problems. The tricky part is that each instance of class A has, as one of its members, an iterator pointing to its own position in a list of A's: ListOfA_s
typedef std::list< A > ListOfA_s;


class A
class A
{
private:
    ListOfA_s::iterator mv_itOwnPosit;

    // ...
};


Are you starting to see the problem? I cannot typedef a list of A's because the compiler hasn't been introduced to class A yet ... and unfortunately I'm storing objects, not pointers ... so simply forward-declaring class A won't do it. Likewise, if I declare class A first, the compiler will complain that one of its members is of an unknown type. Personally I do not see any way out of this other than storing pointers rather than instances, but it's not something I'd be overly happy to do. Unfortunately I see no other way. :( Does anyone have a really brilliant idea, or will I just have to bite into the sour apple and really store pointers to A?

Share this post


Link to post
Share on other sites
Advertisement

class A;

typedef std::list< A > ListOfA_s;

class A
{
private:
ListOfA_s::iterator mv_itOwnPosit;

// ...
};




Since you don't use A yet you can forward declare it as done above.

Share this post


Link to post
Share on other sites
Forward declare class A:
#include <list>

class A;

typedef std::list< A > listOfAs;

class A
{

public:

listOfAs::iterator i;

};


Enigma

Share this post


Link to post
Share on other sites
Well, I'll be damned, it really works! I didn't know you could typedef lists of undefined types?? =0
But come to think of it, internally the lisst probably store pointers anyway, so I guess that's why. Thanks anyway!

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!