简体   繁体   中英

Expected '(' for function-style cast or type construction

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!= ?

This looks weird:

   Room::Room();

I think you want this:

   Room::Room() {}

You should probably at least initialize the member variables however instead of having a blank constructor.

You may consider to change the following constructor to "explicit" in the header (never abuse "explicit" but sometime is needed)

explicit Room(int);

What if there are in your code places where a class accept both "int" or "const Room&" as constructor parameters?

Assume:

Hospital(int n); //Hospital constructor where n is number of rooms
Hospital(const Room& room); //Hospital constructor, hosptial made initially by only 1 room

In this case without a explicit constructor

Hospital sanGrace(3);

the compiler can't tell if you intented

Hospital sanGrace(3);

or

Hospital sanGrace(Room(3));

with "explicit" you are forced to write

Hospital sanGrace(Room(3));

if you want to create SanGrace's Hospital from a room with number 3.

The same applies also for Patient class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM