Sign in to follow this  
Red Ant

[C++] Cyclic inclusion hell

Recommended Posts

Red Ant    471
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
rick_appleton    864

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
Enigma    1410
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
Red Ant    471
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

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