C++ 错误:表达式必须具有类类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9866802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ERROR : Expression must have class type?
提问by Mike
I am writing a program for class that manages a Hotel. This Report1 function is supposed to list all the occupied rooms and which customer is in each room. I have the code written, but i am getting a error in the condition statement of my nested FOR loop. The compiler is underlining iRoomin the loop............ for(int j = 0; j < iRoom.customerIDinRoom.....it is saying the iRoom expression must have a class type, but I gave it a class type when I declared it in the first FOR loop(of type Room). Any suggestioins?
我正在为管理酒店的班级编写程序。这个 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;
}
This is the Room class declaration
这是 Room 类声明
#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
};
回答by juanchopanza
The error is that customerIDInRoom
is an int
, but you are calling a size
method on it. If you are trying to loop from 0 to customerIDInRoom-1
then you can simply remove the size()
call. If you need to keep a range of customerIDInRoom ints (as suggested by your "no pushback" comment in the code), then you would most likely need a standard library container. Which one to use depends on your requirements. All of these have a size()
method.
错误是它customerIDInRoom
是int
,但您正在调用size
它的方法。如果您尝试从 0 循环到customerIDInRoom-1
那么您可以简单地删除size()
调用。如果您需要保留一定范围的 customerIDInRoom 整数(如代码中的“无推回”注释所建议的那样),那么您很可能需要一个标准库 container。使用哪一种取决于您的要求。这些都是有size()
方法的。
回答by Mike DeSimone
The problem is that an int
, which is how you declared customerIDinRoom
, does not have a size()
method, but you're calling it anyway. Declare it as something sane, such as std::vector<int>
and it should work.
问题是 an int
,即您声明的方式customerIDinRoom
,没有size()
方法,但您无论如何都在调用它。将其声明为理智的东西,例如std::vector<int>
它应该可以工作。
Also:
还:
Room iRoom = listofrooms.get(i);
This is copying the room from listofrooms
into iRoom
. This is more work than necessary; you should use a reference instead:
这是将房间从 复制listofrooms
到iRoom
。这是多余的工作;您应该改用参考:
const Room& iRoom(listofrooms.get(i));