Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ERROR : Expression must have class type?

Tags:

c++

loops

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 iRoom in 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?

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

#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
};
like image 900
Mike Avatar asked Mar 17 '26 07:03

Mike


2 Answers

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.

like image 100
juanchopanza Avatar answered Mar 18 '26 20:03

juanchopanza


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.

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:

const Room& iRoom(listofrooms.get(i));
like image 41
Mike DeSimone Avatar answered Mar 18 '26 19:03

Mike DeSimone



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!