0

I'm trying to access the 'room' array via functions in the room class itself, i.e:

 class ship{

    room * rooms[3][4];
 public:

   room ** getrooms(){

       return *rooms;
   }

 }


class room{

  //variables...
  ship * ship_ptr;
  public:
     getShipPtr(); //assume it returns the pointer to the class ship
     void function_x(){
    this->getShipPtr()->getrooms()->rooms[x][y]->doThat();
      }
  }

I'm doing something wrong pointer-wise but I'm not really sure, what could correct the code so I can access the room array out of the ship class? Note: Assume that the rooms are already initiated

13
  • What part of the code is supposed to check to make sure the access to rooms is not out of bounds? Commented Jan 5, 2016 at 20:27
  • I didn't put it here, for simplicity's sake, and because that's not a problem (at least for now) @crashmstr Commented Jan 5, 2016 at 20:29
  • It is tagged C++, hence get used to std::array and std::vector and do not allocate a single value for a member variable (avoid new/delete) Commented Jan 5, 2016 at 20:30
  • @DieterLücking I'm not allowed to use either std::vector or std::array, my professors insist that I take 'this way' Commented Jan 5, 2016 at 20:32
  • 1
    Why do the rooms need to know about the ship? Looks like either a flaw in the design or a circular reference: ship->room->ship->room... It's like having a vector of integers and each integer refers to the vector. Commented Jan 5, 2016 at 20:36

3 Answers 3

3

I'm doing something wrong pointer-wise but I'm not really sure

You are making the false assumption that a 2D array can decay to a pointer to a pointer just like a 1D array can decay to a pointer.

int arr1[10];
int* ptr1 = arr1; // OK. 1D array decays to a pointer.

int arr2[10][20];
int** ptr2 = arr2; // Not OK. 2D array does not decay to a pointer to pointer.

int (*ptr2)[20] = arr2; // OK. ptr2 is a pointer to "an array of 20" objects.

My suggestion:

Simplify your code and change the interface to:

room* getRoom(size_t i, size_t j){
   // Add checks to make sure that i and j are within bounds.
   return rooms[i][j];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, worked like a charm. And you were right, I was assuming that 2D arrays would decay into a pointer like 1D arrays do.
1

I am not going to insist on using std::vectors etc, although your teacher should teach you to use them from the very beginning. So let's stick to writing C in C++. You cannot return a 2D array as a double pointer, as the former does not decay to the latter. If you insist in returning a pointer to your array, what you need is

room* (*getrooms())[4] // define a function returning a pointer to array-4 of room*
{
    return rooms;
}

That is because a 2D array of type e.g. T arr[A][B] decays to a pointer to array-B of T.

Comments

1

I believe the root cause of your issue is the cyclic dependency of having rooms know about the ship and the ship know about the rooms.

I suggest a different design:

class Ship
{
  Room ship_rooms[3][4];  // Note, not dynamically allocated.
  public:
    void move_unit(Room& from_room, Room& destination)
    {
      destination.set_unit(from_room.get_unit());
    }
    void move_unit(unsigned int from_row, unsigned int from_column,
                   unsigned int dest_row, unsigned int dest_column)
    {
      ship_rooms[dest_row][dest_column].set_unit(ship_rooms[from_row][from_column].get_unit());
    }
    Room& get_room(unsigned int row, unsigned int column)
    { return ship_rooms[row][column]; }
};

In the above design, the Ship is in charge of moving units between rooms. Rooms will get (cut) units or set units (receive them). This eliminates the need for a room to know anything about the Ship.

Another possible design is for a room to move something to another room:

class Room
{
  Item unit;
  public:
    void receive_unit(const Room& other)
    {
      unit = other.unit;
    }
    void transfer_unit(Room& other)
    {
      other.unit = unit;
    }
};

The above structure allow rooms to communicate with each other, without knowing anything about the ship or container.

Note: These designs resolve the issue of pointer by not requiring pointers. References are used instead.

Downvoters: Please add explanation in the comment.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.