# [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 on other sites
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 on other sites
Kalasjniekof    246
you can prototype the class at the beginning of your file by typing
class A;

it should be able to recognize the class afterwards.

##### Share on other sites
Kalasjniekof    246
 huh? double post bug? anyway, i was obviously beaten :( [/edit]

##### 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 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!