Forward declare objects in C++?

Hi there! I just made this code and I can see why it isn't working but not how to solve it. So I need to forward declare object "toilet" but I don't know. If you can see what I am going for and have a better approach, please post that too!

Many thanks

/Sigge

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

using namespace std;

struct room{
  string name;
  room(string Name,room& West){
    room& west = West;
  }
};

int main(){
  room kitchen("kitchen", toilet);
  room toilet("toilet", kitchen);
  return 0; 
}
As you have discovered ,you can't use an object before it is created.
So use it after it is created.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>

using namespace std;

struct room{
  string name;
  room(string Name,room& West){
    room& west = West;
  }
  room(string Name){};
  setWest(room& West) { room& west = West;}
};

int main(){
  room kitchen("kitchen");
  room toilet("toilet", kitchen);
  kitchen.setWest(toilet);
  return 0; 
}
Last edited on
Thanks! I will leave this thread open for a few more hours just in case somebody else think they have a better solution. ^^
Consider using pointers. Then you could set it after the objects has been created, and if there is no west room it could be null.
Last edited on
Please, give an example. I am quite slow lol
Line 9 in your OP doesn't make sense. What are you trying to do there?
I am (trying) to connect two rooms together through west or/and east.
Line 9 creates a room-reference and sets it. Then, throws it away. Your class isn't storing anything. You're not doing anything with that information. If you want the class to know which room it is connected to, you need the class to contain some variable that can store that information.
Last edited on
Okay, I don' t know what that was about. I've done quite a few changes and improvements to my code, here is it now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

struct room{
  string name;
  string description;
  string examine;
  room* west;
  room* east;
  room* north;
  room* south;
  room(string Name, string Description, string Examine){
    name = Name;
    description = Description;
    examine = Examine;
  }
  void setDirection(room& West, room& East, room& North, room& South){
    west = &West;
    east = &East;
    north = &North;
    south = &South;
  }
};

void getUserInput(string& x){
  cout << ">";
  getline(cin, x);
  transform(x.begin(), x.end(), x.begin(), ::tolower);
}

int main(){
  string userInput;
  room* currentRoom;
  
  room emptyRoom("empty room", "This room is empty/blocked off.", "It's just black.");
  room kitchen("kitchen","You're standing in a kitchen.", "It's a well-lit kitchen. It offers everything a kitchen might need.");
  room bathroom("bathroom", "You're standing in a bathroom.", "It's a stinky toilet. Somebody must have forgotten to flush.");
  room livingRoom("living room", "You're standing a living room.", "You can't get your eyes of that big televison.");
  room bedroom("bedroom", "You're'standing in a bedroom.","That bed looks comfy.");
  
  kitchen.setDirection(emptyRoom, bathroom, emptyRoom, livingRoom);
  bathroom.setDirection(kitchen, emptyRoom, emptyRoom, bedroom);
  livingRoom.setDirection(emptyRoom, bedroom, kitchen, emptyRoom);
  bedroom.setDirection(livingRoom, emptyRoom, bathroom, emptyRoom);
  
  currentRoom = &kitchen;
  
  while(true){
    cout << endl;
    cout << currentRoom->description;
    cout << endl;
    getUserInput(userInput);
    if(userInput == "west" && currentRoom->west != &emptyRoom){
      currentRoom = currentRoom->west;
    }
    else if(userInput == "east" && currentRoom->east != &emptyRoom){
      currentRoom = currentRoom->east;
    }
    else if(userInput == "north" && currentRoom->north != &emptyRoom){
      currentRoom = currentRoom->north;
    }
    else if(userInput == "south" && currentRoom->south != &emptyRoom){
      currentRoom = currentRoom->south;
    }
    else if(userInput == "examine"){
      cout << currentRoom->examine;
    }
    else{
      cout << "You can't go that way!" << endl;
    }
  }
  
  return 0;
}
I will mark this thread as solved now. This is pretty much my final code. If you have anything else you would like me to know please send me a pm. : )
Topic archived. No new replies allowed.