Sorry for my inexperience in the matter and if its a simple answer, but I am writing a C++ program for an assignment and cannot for the life of me find a solution.
I have created a class called Door that possesses a local variable that stores the room it's connected to. Additionally, each door is connected to one another through another local variable. The local variable that records the door connected to each door is a pointer (a requirement of the assignment).
The door class passes back the door its connected to through dereferencing the pointer (through the * operator).
Another class, Room, stores four doors in variables names after the four directions (North, East, etc) and is identified by a simple integer that can be passed through a function to receive a shared pointer that points to the room object.
The problem I am having is in my class that is responsible for creating the rooms and populating them with doors, then connecting the doors to one another, the program is able to set the local variables of the Door class, however, once the door object that's variables were just set is referred back to it appears to have incorrectly set variables.
In the example below the integers of the rooms that are being created are 1 and 2.
This is not the full code, just a snippet of relevant info
class Door{
private:
Door *_connectedDoor;
int _connectedRoom;
bool _isEntrance;
bool _isExit;
};
void Door::connectDoor(Door connectedDoor){
if( _isExit == true || _isEntrance == true)
{
std::cout << "Cannot connect, is an entrance / exit";
}
else
{
_connectedDoor = &connectedDoor;
}
};
Door Door::getConnectedDoor(){
return *_connectedDoor;
}
int Door::getRoom(){
std::cout << std::endl << "*** passing connected room:" << _connectedRoom << " ***" << std::endl;
return _connectedRoom;
};
void Door::setRoom(int room){
std::cout << std::endl << "*** setting connected room:" << room << " ***" << std::endl;
_connectedRoom = room;
};
class Room{
private:
int _id;
std::string _description;
Door _northDoor;
};
void Room::setNorth(Door north){
_isDoorNorth = true;
_northDoor = north;
std::cout << std::endl << "*** configuring doors room internally to:" << _id << " ***" << std::endl;
north.setRoom(_id);
};
class Game
void Game::connectRooms(int room1, int room2, std::string startingDirection)
{
std::cout << std::endl << "*** connecting rooms ***" << std::endl;
std::shared_ptr<dungeon::Room> startingRoom = _currentDungeon.retrieveRoom(room1);
std::shared_ptr<dungeon::Room> endingRoom = _currentDungeon.retrieveRoom(room2);
dungeon::Door startingRoomDoor = *new dungeon::Door();
dungeon::Door endingRoomDoor = *new dungeon::Door();
std::cout << std::endl << "*** configuring room:" << startingRoom->id() << " ***" << std::endl;
std::cout << std::endl << "*** configuring room:" << endingRoom->id() << " ***" << std::endl;
startingRoom->setNorth(startingRoomDoor);
endingRoom->setSouth(endingRoomDoor);
std::cout << std::endl << "*** configuring doors room:" << startingRoomDoor.getRoom() << " ***" << std::endl;
std::cout << std::endl << "*** configuring doors room:" << endingRoomDoor.getRoom() << " ***" << std::endl;
startingRoomDoor.connectDoor(endingRoomDoor);
endingRoomDoor.connectDoor(startingRoomDoor);
};
Output:
*** connecting rooms ***
*** configuring room:1 ***
*** configuring room:2 ***
*** configuring doors room internally to:1 ***
*** setting connected room:1 ***
*** configuring doors room internally to:2 ***
*** setting connected room:2 ***
*** configuring doors room:
*** passing connected room:1835344 ***
1835344 ***
*** configuring doors room:
*** passing connected room:1835344 ***
1835344 ***
Expected:
*** configuring doors room:
*** passing connected room:1 ***
1 ***
*** configuring doors room:
*** passing connected room:2 ***
2 ***