0

I am facing the following problem. I have the following classes, Room and Reservation . For Reservation class there is a function (void Reservation :: rsrvRoomAssignment(Room* r)) that assigns the Room* rsrvRoom member of Reservation class, to a Room object. I want though to call this class from inside the Room object but i have no clue on how to achieve that properly passing as argument the created object that runs the code.

The code describes the above:

Reservation.h

class Room;

class Reservation {
public:
    static int rsrvCode;
    string rsrvName;
    unsigned int rsrvDate;
    unsigned int rsrvNights;
    unsigned int rsrvPersons;
    Room* rsrvRoom;             // Room assigned to reservation.
    Reservation();
    ~Reservation();
    void rsrvRoomAssignment(Room*);

};

Reservation.cpp

#include <iostream>
#include "Reservation.h"

using namespace std;

/* 
    Constructor & Destructor 
*/

void Reservation :: rsrvRoomAssignment(Room* r){
    rsrvRoom=r;
}

Room.h

#include "Reservation.h"
class Room {
public:
    static unsigned int roomNumber;
    unsigned int roomCapacity;
    Reservation* roomAvailability[30];
    double roomPrice;
    Room();
    ~Room();
    bool roomReservationAdd(Reservation*);
};

Room.cpp

#include <iostream>
#include <string>
#include "Room.h"

using namespace std;

/*
    Constructor & destructor
*/

bool Room::roomReservationAdd(Reservation* r){
    /* some statement that returns flase */
    r->rsrvRoomAssignment(/* & of created object */); // This is the problem i described.
    return 1;
}

I am pretty new to OOP so there might be some more logical errors on the above snipsets, so don't be harsh :) .

Thanks for any kind of help!

Sarriman
  • 382
  • 5
  • 22
  • 2
    `r->rsrvRoomAssignment(this->Room);`?? `Room` doesn't have a member called `Room`? Did you mean `r->rsrvRoomAssignment(this);`? – πάντα ῥεῖ May 20 '17 at 12:44
  • @πάνταῥεῖ The idea I am trying to describe with the comments on `Room.cpp`, is that I want to pass as an argument to `rsrvRoomAssignment` the created object of `Room` class that will call `r->rsrvRoomAssignment` – Sarriman May 20 '17 at 12:50
  • You shouldn't mess around with pointers in c++. If you need to pass a reference somewhere, rather use a reference parameter (`&`). Also post a [MCVE] that reproduces your problem, all error messages included verbatim. – πάντα ῥεῖ May 20 '17 at 12:52
  • @πάνταῥεῖI think you are misunderstanding me and it's my fault. I know that `this->Room` is wrong, and I only pasted it there to make understandable the nature of my question. I did it Since I am not familiar with many terms in OOP and in C++. My goal is only to describe the idea I am trying to implement. – Sarriman May 20 '17 at 12:55
  • @πάνταῥεῖI made an edit to my post, since I think I wasn't making clear my issue. Thank you for pointing me to it! – Sarriman May 20 '17 at 13:00
  • Sounds like you're thinking too complicated. Why would you like to assign `Room` from `Reservation` and not just vice versa. – πάντα ῥεῖ May 20 '17 at 13:00
  • 2
    You might be interested in our [list of good C++ books](http://stackoverflow.com/q/388242/1782465) for familiarising yourself with the terms better. – Angew is no longer proud of SO May 20 '17 at 13:00
  • `void rsrvRoomAssignment(Room*);` expects an `Room*`, so passing `this` in `bool Room::roomReservationAdd(Reservation* r)` could solve the problem. – frogatto May 20 '17 at 13:07

1 Answers1

2

When inside a class method, this indicates the instance of the object calling it. In your case, when a room instance X calls X.roomReservationAdd(r), this points to the room instance X. Hence, you can simply call r->rsrvRoomAssignment(this);

frogatto
  • 28,539
  • 11
  • 83
  • 129
user1620443
  • 784
  • 3
  • 14