简体   繁体   English

函数式强制转换或类型构造需要 '('

[英]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.有时没有问题,但后来又回来了……这里的其他用户定义对象包括一个没有问题的 Patient 类和一个也没有问题的 LinkedList 模板类。

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和 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.问题出在 Room(int) 构造函数上。 Xcode keeps giving me a message saying Expected '(' for function-style cast or type construction Xcode 不断给我一条消息,说对于函数式强制转换或类型构造,预期为 '('

I have no idea what's going on我不知道是怎么回事

You clearly forgot to include the header that defines Patient :您显然忘记包含定义Patient的标头:

 #include "Patient.h"

or similar.或类似。

Also,还,

patient= Patient();

is redundant, the member patient will be value-initialized by default, and是多余的,默认情况下成员patient将进行值初始化,并且

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!= ?在您对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”(不要滥用“explicit”,但有时需要)

explicit Room(int);

What if there are in your code places where a class accept both "int" or "const Room&" as constructor parameters?如果在您的代码中某个类同时接受“int”或“const Room&”作为构造函数参数怎么办?

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.如果您想从编号为 3 的房间创建 SanGrace 医院。

The same applies also for Patient class.这同样适用于 Patient 类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++ Xcode期望&#39;(&#39;用于函数样式的强制转换或类型构造 - c++ Xcode expected '(' for function-style cast or type construction C++:“预期的 &#39;(&#39; 用于函数式强制转换或类型构造”错误 - C++: “Expected '(' for function-style cast or type construction” Error Xcode:错误:对于函数样式转换或类型构造,应为“(” - Xcode: error: expected '(' for function-style cast or type construction 对于函数式强制转换或类型构造,我该如何解决这个预期的 &#39;(&#39;? - How do I fix this Expected '(' for function-style cast or type construction? G++ 命令抛出“expected '(' for function-style cast or type construction” - G++ command throws “expected '(' for function-style cast or type construction” “Expected &#39;(&#39; for function-style cast or type construction”错误是什么意思? - What does the "Expected '(' for function-style cast or type construction" error mean? &#39;(&#39;用于函数样式转换或构造类型Xcode错误 - '(' for function-style cast or construction type Xcode error 函数式强制转换与构造函数 - Function-style cast vs. constructor C风格的演员是否与功能风格的演员阵容相同? - Is a C-style cast identical to a function-style cast? 转换为枚举类型需要显式强制转换(static_cast,C样式强制转换或函数样式强制转换) - Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM