Hi,

Not sure if I'm doing something stupid here or not, I want a map of integers and objects (Rooms) which I've done, but whenever I create a new room to add to the map it appears to use the same memory location as the previous Room(s). The upshot being I have a map of n 'Rooms' but they're all the same object.

Code:

void CMazeBuilder::BuildRoom(int rid, string desc) {
    Room * room = new Room(desc);
    cout<<"New Room Made: "<<room->getDescription()<<endl;
    cout<<"This room is stored at: "<< &room <<endl;
    roomsMap.insert(make_pair(rid, room));
} //end of BuildRoom

Sample Output (When the above function is called 3 times):

New Room Made: Room1
This room is stored at: 0x23fe5c
New Room Made: Room2
This room is stored at: 0x23fe5c
New Room Made: Room3
This room is stored at: 0x23fe5c

Does anyone know what I am doing wrong?

Thanks

You're printing the address of the pointer, not the address of the memory pointed to. Remove the address-of operator from your cout statement and you'll get the address of the memory that new gives you:

cout<<"This room is stored at: "<< room <<endl;
This question has already been answered. Start a new discussion instead.