So I'm trying to create a class Room that simulates a hospital room but it keeps giving me an error in my constructor. Sometimes there's no issue but then it comes back....other user defined objects here include a Patient class that has no problems and a LinkedList template class that also has no issues.
Here's the header
class Room
{
public:
Room();
Room(int);
static LinkedList<Room> createRooms();
Patient patient;
int roomNumber;
bool operator==(const Room &other) const; //overload ==
bool operator!=(const Room &other) const; //overload !=
void operator=(const Room &other) const; //overload =
};
and the cpp
#include "Room.h"
Room::Room();
Room::Room(int n)
{
roomNumber= n;
patient= Patient();
}
LinkedList<Room> Room::createRooms() {
//create rooms up until ROOMS is reached
LinkedList<Room> roomList;
for(int i= 1; i < 11; i++){
Room room= Room(100+i);
roomList.push(room);
}
return roomList;
}
//Overload ==
bool Room::operator==(const Room &other)const{
//compare each room's room number field
return (this->roomNumber == other.roomNumber && this->patient == other.patient);
}
//Overload !=
bool Room::operator!=(const Room &other)const{
return !(this == &other);
}
void Room::operator=(const Room &other)const{
this->patient= other.patient;
this->roomNumber= other.roomNumber;
}
the problem is with the Room(int) constructor. Xcode keeps giving me a message saying Expected '(' for function-style cast or type construction
I have no idea what's going on
You clearly forgot to include the header that defines Patient:
#include "Patient.h"
or similar.
Also,
patient= Patient();
is redundant, the member patient will be value-initialized by default, and
Room::Room();
is not correct - you're not providing an implementation.
Next, your design seems flawed. You seem to imply that a patient is part of a room, and chose composition to do so. But it's not. What if the room is empty? Your current design doesn't yet treat that case.
EDIT: did you mean:
return !(*this == other);
in your overload to operator!=?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With