gpt4 book ai didi

c++ - 错误 : Expression must have class type?

转载 作者:行者123 更新时间:2023-11-30 00:38:48 24 4
gpt4 key购买 nike

我正在为管理酒店的类(class)编写程序。此 Report1 函数应该列出所有已占用的房间以及每个房间中的客户。我已经编写了代码,但我在嵌套的 FOR 循环的条件语句中遇到错误。编译器在循环中为 iRoom 加上下划线...... for(int j = 0; j <iRoom.customerIDinRoom.... .它说 iRoom 表达式必须有一个类类型,但我在第一个 FOR 循环(类型为 Room)中声明它时给了它一个类类型。有什么建议吗?

string Hotel::Report1()
{

string result;
for(int i=0;i<listofrooms.size();i++)
{
Room iRoom = listofrooms.get(i);
result+= padLeft(intToString(iRoom.roomID),' ',8)+" "+
padRight(iRoom.name,' ',20) + " "+
padLeft(intToString(iRoom.floor),' ',8) + " " +
padLeft(intToString(iRoom.number),' ',8) + " " +
padLeft(intToString(iRoom.basePriceInSeason),' ',8) + " " +
padLeft(intToDollarString(iRoom.basePriceOutOfSeason),' ',8) + "\n";

for(int j = 0; j < iRoom.customerIDinRoom.size(); j++)
{
int cusID= iRoom.customerIDinRoom[j];
Customer & cus = listofcustomers.getByID(cusID);
result+= padLeft(intToString(cus.customerID),' ',18)+" "+
padRight(cus.name,' ',20) + " "+
padRight(cus.phoneNumber,' ',10) + " " +
padRight(cus.ccNumber,' ',20) + "\n";

}
}
return result;
}

这是房间类声明

#include <iostream>
#include <string>
using namespace std;

class Hotel;

class ListOfRooms;

class Room
{
friend class ListOfRooms;
friend class Hotel;
public:
Room(string n,int flo,int num,int bpin, int bpos);
Room();
void addCusID(int cusID){customerIDinRoom = cusID;}
void removeCustomerID(int cusID) { customerIDinRoom = 0;}

private:
string name; //BUILDING
int floor;
int number;
int basePriceInSeason;
int basePriceOutOfSeason;
int roomID;
int customerIDinRoom; //not pushback, will be assignment
};

最佳答案

错误是 customerIDInRoom 是一个 int,但您正在对其调用 size 方法。如果您尝试从 0 循环到 customerIDInRoom-1,那么您只需删除 size() 调用即可。如果您需要保留一系列 customerIDInRoom 整数(如代码中的“无推回”评论所建议的那样),那么您很可能需要 standard library container .使用哪一个取决于您的要求。所有这些都有一个 size() 方法。

关于c++ - 错误 : Expression must have class type?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9866802/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com