Thread: Pointer to object help

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    73

    Pointer to object help

    I posted a thread asking for help on this some time ago, and I've been able to fix some errors.

    I'm getting the errors below in regards to the function posted below. The idea is that the will use a generic class for all four room types to inherit from. Problem is, these are the only errors left, and I can't seem to fix them. Can someone tell me what I'm doing wrong?

    Errors:
    Code:
    1>c:\users\trey\documents\visual studio 2010\projects\oop program 1\oop program 1\hotel.cpp(28): error C2061: syntax error : identifier 'Penthouse'
    1>c:\users\trey\documents\visual studio 2010\projects\oop program 1\oop program 1\hotel.cpp(31): error C2061: syntax error : identifier 'Suite'
    1>c:\users\trey\documents\visual studio 2010\projects\oop program 1\oop program 1\hotel.cpp(34): error C2061: syntax error : identifier 'King'
    1>c:\users\trey\documents\visual studio 2010\projects\oop program 1\oop program 1\hotel.cpp(37): error C2061: syntax error : identifier 'DoubleRm'
    hotel.h:
    Code:
    #include "Room.h"
    
    #pragma once
    class Hotel
    {
    	int hotelID;
    	static const int MAX_SIZE = 101;
    
    
    public:
    	Hotel(void);
    	~Hotel(void);
    	Room **room;
    	void setHotelID (int ID);
    	int getHotelID();
    	void setHotelRooms(int max, int min, char type);
    	int checkOpenRooms();
    	void checkin(Customer c, Room *r);
    	void checkout(Room r);
    };
    setHotelRooms:
    Code:
    void Hotel::setHotelRooms(int max, int min, char type)
    {
    	room = new Room *[MAX_SIZE];
    	for (int c = min; c<=max; c++)
    	{
    		switch (type)
    		{
    		case 'P':
    			room[c] = new Penthouse;
    			break;
    		case 'S':
    			room[c] = new Suite;
    			break;
    		case 'K':
    			room[c] = new King;
    			break;
    		case 'D':
    			room[c] = new DoubleRm;
    			break;
    		default:
    			break;
    		}
    	}
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    This seems like a silly question, but: do you have classes called Penthouse, Suite, King, and DoubleRm?

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    73
    Yes. I'll post the five .h files (Room, and the above 4), in case they'll help.

    Room.h:
    Code:
    class Room	{
    	private:
    		int occNum;
    		int nights;
    		int MaxOccNum;
    		double baseRate;
    		double addRate;
    		bool smoking;
    
    
    	public:
    		Room(void);
    		~Room(void);
    		Customer customer;
    		void setSmoking(int i, int s);
    		bool isOccupied();
    		bool isSmoking();
    		int getNights();
    		void setNights(int days);
    		int getOccNum();
    		void setOccNum(int ppl);
    		int getMaxOccNum();
    		void setMaxOccNum(int max);
    		double getBaseRate();
    		void setBaseRate(double rate);
    		double getAddRate();
    		void setAddRate(double add);
    		double calculateBill(int night, int occNum, double rate, double add);
    		void assignCustomer(string nm, string cc, bool emp);
    		void modify();
    		void clearRoom();
    	};
    Penthouse.h:
    Code:
    class Penthouse: private Room{
    private:
    	bool butler;
    	double ButlerPay;
    public:
    	Penthouse(double base, double add);
    	~Penthouse(void);
    	bool getButler();
    	void setButler(char b);
    };
    Suite.h:
    Code:
    class Suite: private Room{
    private:
    
    
    public:
    	Suite(double base, double add);
    	~Suite(void);
    };
    King.h:
    Code:
    class King: private Room{
    private:
    	
    public:
    	King(double base, double add);
    	~King(void);
    };
    DoubleRm.h:
    Code:
    class DoubleRm: private Room{
    private:
    
    
    public:
    	DoubleRm(double base, double add);
    	~DoubleRm(void);
    };

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So if you think you've defined the classes, but the compiler doesn't, that means you haven't told the compiler about those classes. You need to include all the .h files in your hotel.cpp so the compiler knows they exist. (Just having them in the project is not going to be enough; that's for your benefit not the compiler's.)

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    73
    Thank you. Now, it's turning up the error C2243 (type cast), in that "conversion from "<type> *" to "Room *" exists, but is inaccessible. How do I solve that?

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    why you are using private inheritance? try public
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    73
    Appears to be one final error. Unresolved externals for both calculateBill function calls below.

    ERROR:
    Code:
    1>Main.obj : error LNK2019: unresolved external symbol "public: double __thiscall Room::calculateBill(int,int,double,double)" (?calculateBill@Room@@QAENHHNN@Z) referenced in function _main
    1>C:\Users\Trey\documents\visual studio 2010\Projects\OOP Program 1\Debug\OOP Program 1.exe : fatal error LNK1120: 1 unresolved externals
    Code:
    Code:
            case 'E':
                cout << "Which room is being checked out?  ";
                cin >> rn;
                total = hotel.room[rn]->calculateBill(hotel.room[rn]->getNights(), hotel.room[rn]->getOccNum(), hotel.room[rn]->getBaseRate(), hotel.room[rn]->getAddRate());
                hotel.room[rn]->customer.discount(total);
                cout << "TOTAL:  $" << total <<  "." << endl;
                cout << "CC#:  " << hotel.room[rn]->customer.getCCNum();
                hotel.room[rn]->clearRoom();
                break;
    Function:
    Code:
    double calculateBill(int night, int occNum, double base, double add){
        double charge;
        double addRate = add*(occNum-1);
        charge = addRate+base; 
        charge = charge*night;
    
    
        return charge;
    }

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You are calling the function as though it belongs to a class, but your declaration is for a just plain function.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    For anyone else wondering about the solution, you must tell the compiler that the function is part of the Room class:

    double Room::calculateBill(int night, int occNum, double base, double add)

    You should also learn to use a std::vector instead of new/delete. Your Hotel class is pretty broken because you have violated the Law of Pointers (yes, I made up that name) [you need a copy constructor, copy assignment operator and a destructor]. Basically, if you try to copy your class, you will end up with undefined behavior most likely, and if not, then with memory leaks. So use std::vector and save yourself the trouble!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    you have violated the Law of Pointers (yes, I made up that name)
    The existing name in the literature is the rule of three, or variants thereof.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    As a base class, should Room's destructor be made virtual?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by laserlight View Post
    The existing name in the literature is the rule of three, or variants thereof.
    True, when thinking of the Rule Of Three, I somehow completely forgot that there was a destructor. Yes, in this case, the Rule of Three should apply. I was thinking there was no destructor, in which case the Rule of Three hadn't been violated. Of course, in that case, one might probably wish to invoke it anyway,

    Quote Originally Posted by hk_mp5kpdw View Post
    As a base class, should Room's destructor be made virtual?
    No, since Base is only used as a base class for private inheritance. You can't use a pointer to the base class with private inheritance, hence you can't delete a derived object pointing to a base class, hence no need for virtual.
    But if public inheritance is used, then yes. Still, I am going to agree that the OP should think about this as a design decision. Should Room only be used as a base class with private inheritance? If so, no virtual destructor is necessary. But if it might be used in public inheritance in the future, then one should be used.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    No, since Base is only used as a base class for private inheritance.
    Hmm... well, that was true in the original code, but my guess is that after having read vart's post #6, Trey Brumley changed the code to use public inheritance, hence hk_mp5kpdw's concern is legitimate.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How many pointer are pointing to this object
    By Hannibal2010 in forum C Programming
    Replies: 7
    Last Post: 10-10-2011, 08:10 PM
  2. Replies: 5
    Last Post: 04-23-2008, 01:33 PM
  3. pointer to struct object in class object
    By Stevo in forum C++ Programming
    Replies: 3
    Last Post: 02-25-2004, 07:58 PM
  4. trouble with pointer to object
    By drb2k2 in forum C++ Programming
    Replies: 3
    Last Post: 04-16-2003, 03:10 PM
  5. Replies: 2
    Last Post: 10-12-2002, 07:44 PM