user3473161 user3473161 - 3 years ago 106
C++ Question

How to assign an object to a property of an object in a vector with raw pointers?

This question is probably going to be a noob question because I have just started out with C++. But I cannot find out what I am doing wrong. The project that I am working on is an assignment for school. In this assignment, we are only allowed to use raw pointers, so we find out how raw pointers work.

Now those raw pointers gave me some trouble. Those pointers gave me the error: 'no suitable conversion function from "Room" to "Room *" exists'.

To try and explain how I caused this error I will try to give a stripped-down version of the code that I have written.

class Level
Room _startRoom;
vector<vector<Room>> _rooms;

class Room
Room *North;
Room *East;
Room *South;
Room *West;

I have a class named Level this is a class with a vector in a vector to be able to print out the Rooms next to each other neatly. Not every room is connected, so I gave the Room direction-properties for the connections with the other rooms. But when I try to generate the connections between the rooms, by setting _rooms[y][x].South with _rooms[y+1][x]. I got the error: 'no suitable conversion function from "Room" to "Room *" exists'.

Could someone clarify why I am getting this error message? All help will be appreciated.

Answer Source

The expression _rooms[y + 1][x] is a Room instance, not a pointer to a room. To get a pointer to it you need to use the address-of operator &:

_rooms[y][x].South = &_rooms[y+1][x];

On an unrelated note: You do remember to populate the vectors in your Level object?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download