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

Recommended Answers

All 2 Replies

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;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.