3

This is probably a dumb question. I am trying to make a text-mud. I need each Room class to contain other Room classes that one can refer to when trying to move to them or get information from them. However, I can not do that because I obviously can not declare a class within its definition. So, how do I do this? Here's what I mean when I state I can not do it:

class Room {
    public:
        Room NorthRoom;
        Room EastRoom;
        Room SouthRoom;
        Room WestRoom;
};
2
  • Thanks for the answers. I forgot about pointers. They're a new concept to me after being spoiled by the luxurious laziness of high-level languages.
    – Reznor
    Commented Oct 23, 2010 at 15:29
  • 2
    If you are new to C++, you may want to get a good introductory book, even if you have experience with other languages. In addition to the answers below, take a look at "smart pointers" (you can search here on Stack Overflow or on Google), which make memory management in C++ far simpler. Commented Oct 23, 2010 at 15:33

4 Answers 4

5

It's not possible to have a Room member variable. You could use a pointer or reference though.

class Room {
    public:
        Room* NorthRoom;
        Room* EastRoom;
        Room* SouthRoom;
        Room* WestRoom;
};
2
  • 4
    It's more fundamental than the class not being complete. If it were possible, the resulting class would be of infinite size! Commented Oct 23, 2010 at 15:25
  • @Oli: Yep. I've deleted that immediately after the post is submitted as I've realized it is not the cause.
    – kennytm
    Commented Oct 23, 2010 at 15:26
2

I am sure not EVERY room has four children rooms, right? Otherwise the number of your rooms is infinity which is hard to handle in finite memory :-)

You might try

class Room {
    public:
        Room* NorthRoom;
        Room* EastRoom;
        Room* SouthRoom;
        Room* WestRoom;
};

Then you can have NULL pointers when a room doesn't have children.

1

Your Room needs to have pointers to other Rooms (that is, Room*s).

A class type object (like Room) has a size that is at least large enough to contain all its member variables (so, if you add up the sizes of each of its member variables, you'll get the smallest size that the class can be.

If a class could contain member variables of its own type then its size would be infinite (each Room contains four other Rooms, each of which contains four other Rooms, each of which contains...).

C++ doesn't have reference type objects like Java and C#.

0

You should use pointers:

class Room {
    public:
        Room* NorthRoom;
        Room* EastRoom;
        Room* SouthRoom;
        Room* WestRoom;
};

Probably cause is that class does not yet its constructor, so when you use pointers you init them later, when class has construcotr definition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.