   Predeclartion of classes?
  I have two classes, each which has a member of the other, so I need to predeclare one of the two.class Room{   public:   Area MyArea;}class Area{   public:   Room MyRoom;}I'm sure I can find away around this, but it is easier for me this way.So... is there a way to predeclare a class, or do I put them both a their own header and #include each other?   
Quote:Original post by Servant of the LordI have two classes, each which has a member of the other, so I need to predeclare one of the two.class Room{   public:   Area MyArea;}class Area{   public:   Room MyRoom;}I'm sure I can find away around this, but it is easier for me this way.So... is there a way to predeclare a class, or do I put them both a their own header and #include each other?I don't think that's possible.  And if it was, you'd loop infinately:Room has an area.Room's area has a room.Room's area's room has an area.Room's area's room's area has a room.And so on.You can declare an incomplete type:class name;And then you can have pointers/references to it without have it implemented yet.   ;  It is perfectly legal to forward declair a class, just use pointers to the class:class Area;class Room{   ...   Area *myArea;}class Area{   ...   Room *myRoom;}However, in this case, it seems to me that your overall design needs some reworking, to avoid this problem all together.   ;  /me pats agi_shi on the headThe young padawan learns quickly... and correctly! [smile] That said, it's called a forward declaration, not a "predeclaration." And you can not have an object contain an instance of a type that contains an instance of the original object type. That'd explode in recursive inclusions - the size of the object would be infinite.You may only have a pointer or reference to type in this instance. Fix the design, SoL.   ;  Hmm, it would loop. Okay, I have a way around the problem, thanks. I'll just change the way the classes are used in my program.   ;  That's the same problem with two functions that must call each other. Simple define them before using them:class Room;class Area;class Room{   public:   Area MyArea;}class Area{   public:   Room MyRoom;}It won't loop O.o It doesn't make much sence if you think about that... This is just a declaration, you aren't even using the classes.The compiler will "read" as this:// In fact, a class called Room exists...// In fact, a class called Area exists...// Ah! Here is the Room class that they said that exists before... Lets see.. Area MyArea;?? I've never seen this class, but they said it exist just before, don't display any errors.// Aha! And here is the other class..! A public declaration of a member of type Room. Room is already declared. No errors! :DThis is perfectly legal. If you do something like this:class Room;class Area;class Room{   public:   Area MyArea;}And forget to declare the class Area, it will display and error, of course, but the error isn't with the line Area MyArea;, its with "class Area;", that fool the compiler saying that something inexistent exists.Cya!   ;Quote:Original post by n2_liquidThat's the same problem with two functions that must call each other. Simple define them before using them:class Room;class Area;class Room{   public:   Area MyArea;}class Area{   public:   Room MyRoom;}It won't loop O.o Yes it will. As Oluseyi pointed out, the sizeof a room class and an area class is infinite, since they both include each other. This isn't an issue if you use pointers, but I still believe the best solution is to redesign the classes.;Quote:Original post by n2_liquidThat's the same problem with two functions that must call each other. Simple define them before using them:class Room;class Area;class Room{   public:   Area MyArea;}class Area{   public:   Room MyRoom;}It won't loop O.o It doesn't make much sence if you think about that... This is just a declaration, you aren't even using the classes.You misunderstand.Room contains an Area. Which contains a Room. Which contains an Area. Which contains a Room... ad infinitum, such that the effective size of the object is infinite. That's what loops: your object's size determination.The correct solution is as follows:class Area;class Room{public:  Area * area;};class Area{public:  Room * room;};;  I think they ment it would loop as Area.Room.Area.Room.Area.Room.Area etc...Doesn't matter anyway, I had my game logic wrong, and didn't need to do that anyhow.I got a new(unrelated) problem, though:Room NULL_ROOM(0,0,0,0,0,0,0,0,0,0); //The 'NULL' of roomsNULL_ROOM.Description("This is 'Room Empty', and should not be accessable.",   "If you are seeing this message, something went wrong.");NULL_ROOM.Set("Here", NULL_ITEM, 0);This is giving me two "syntax error before '.' token" messages. It is in a cpp file, so that's not the problem.The class 'Room' looks like this://classes_H.hclass Room{    public:    Room(bool N, bool NE, bool E, bool SE, bool S, bool SW, bool W, bool NW, bool U, bool D);    ~Room();   void Description(std::string line_one); //Description of the room given to player    void Description(std::string line_one, std::string line_two);    void Description(std::string line_one, std::string line_two, std::string line_three);    void Description(std::string line_one, std::string line_two, std::string line_three, std::string line_four);    void Description(std::string line_one, std::string line_two, std::string line_three, std::string line_four, std::string line_five);   void Sound();   void Set(std::string SetArea, Item SetRoomItem, int SetRoomNum);   int Number_of_lines;   std::string Line[5];   std::string Area; //The name of the area the room is in (example: 'Central cave')   Item RoomItem; //Holds what item is in the room   int RoomNum; //Holds the room's ID number   bool NORTH;    bool NORTHEAST;    bool EAST;    bool SOUTHEAST;    bool SOUTH; //Can you go south? 0 = NO 1 = YES    bool SOUTHWEST;    bool WEST;    bool NORTHWEST;    bool UP;    bool DOWN;};And here are the functions://classes_CPP.cppRoom::Room(bool N, bool NE, bool E, bool SE, bool S, bool SW, bool W, bool NW, bool U, bool D){    NORTH = N;    NORTHEAST = NE;    EAST = E;    SOUTHEAST = SE;    SOUTH = S;    SOUTHWEST = SW;    WEST = W;    NORTHWEST = NW;    UP = U;    DOWN = D;}Room::~Room(){ };void Room::Set(std::string SetArea, Item SetRoomItem, int SetRoomNum){    Area = SetArea;    RoomItem = SetRoomItem;   RoomNum = SetRoomNum;}void Room::Sound() //Gives the description of the room{    int i = Number_of_lines;    bool stop = false;    while(!stop)    {   std::cout << Line << std::endl;   i--;   if(i = 0){stop = true;}    }}void Room::Description(std::string line_one) //Sets the description of the room{    Line[0] = line_one;    Number_of_lines = 1;}void Room::Description(std::string line_one, std::string line_two){    Line[0] = line_one;    Line[1] = line_two;    Number_of_lines = 2;}void Room::Description(std::string line_one, std::string line_two, std::string line_three){    Line[0] = line_one;    Line[1] = line_two;    Line[2] = line_three;    Number_of_lines = 3;}void Room::Description(std::string line_one, std::string line_two, std::string line_three, std::string line_four){    Line[0] = line_one;    Line[1] = line_two;    Line[2] = line_three;    Line[3] = line_four;    Number_of_lines = 4;}void Room::Description(std::string line_one, std::string line_two, std::string line_three, std::string line_four, std::string line_five){    Line[0] = line_one;    Line[1] = line_two;    Line[2] = line_three;    Line[3] = line_four;    Line[4] = line_five;    Number_of_lines = 5;}Any idea what I'm doing wrong?   ;  Err..Sorry by that, now I understood your point. But because pointers always have a fixed size(normally 4 bytes) it will work nice. =) You're correct Oluseyi, I am so sorry for the confusion..Cya!---EDIT---"void Description(std::string line_one); //Description of the room given to player    void Description(std::string line_one, std::string line_two);    void Description(std::string line_one, std::string line_two, std::string line_three);    void Description(std::string line_one, std::string line_two, std::string line_three, std::string line_four);    void Description(std::string line_one, std::string line_two, std::string line_three, std::string line_four, std::string line_five);"You won't need to do this. You can put an "unlimited" number of arguments.. I don't remember how to do that, but I remember that I saw that on a book... Anybody help here? I am curious xPP Its something like "void Description(std::string line_one,...);"??Bye!---EDIT 2---Can't figure why its showing this messages, anyway x.x[Edited by - n2_liquid on June 21, 2006 5:20:48 PM]