因此,我正在尝试创建一个模拟医院病房的教室,但它在构造函数中始终给我一个错误。有时没有问题,但随后又回来了。...其他用户定义的对象包括没有问题的Patient类和没有问题的LinkedList模板类。

这是标题

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 =



};

和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;
}

问题出在Room(int)构造函数上。 Xcode不断给我一条消息,说函数样式或类型构造应为'('

我不知道发生了什么事

最佳答案

您显然忘记了包含定义Patient的 header :

 #include "Patient.h"

或类似。

也,
patient= Patient();

是多余的,默认情况下将对成员patient进行值初始化,并且
Room::Room();

不正确-您未提供实现。

接下来,您的设计似乎有缺陷。您似乎暗示患者是房间的一部分,因此选择了组合。但事实并非如此。如果房间是空的怎么办?您当前的设计尚未解决该问题。

编辑:您的意思是:
return !(*this == other);

在您的operator!=重载?

07-24 09:36
查看更多