Like Tree1Likes
  • 1 Post By hk_mp5kpdw

Nested Class Construction problem

This is a discussion on Nested Class Construction problem within the C++ Programming forums, part of the General Programming Boards category; In this program, we use nested classes to represent rooms and customers in hotels. My current problem occurs in implementation, ...

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

    Nested Class Construction problem

    In this program, we use nested classes to represent rooms and customers in hotels. My current problem occurs in implementation, but I'm including the header file in case you guys see an error I don't in that.

    My error comes in the implementation where I try to construct a customer with strings x and y. At x, there's a red line with the notation "Expected a ')'"; at y, there's a red line with the notations "this declaration has no storage class or type specifier"; and at the second ), the notation states "this declaration has no storage class or type specifier; expected a ';'".

    What's causing this an how can I fix it?

    Here is the header.
    Code:
    #pragma onceclass hotel
    {
    
    
    public:
        hotel(void);
        ~hotel(void);
        class customer
        {
            const string name;
            string CCNum;
        public:
            customer(string x, string y);
            ~customer(string x, string y);
            void changeCCNum (string x);
            void getCCNum (string x);
            void getName (string y);
            void printReceipt (double z, string x);
        };
        class room
        {
            const int RNum;
            const bool isSmoking;
            double bill;
            int occNum;
            int nights;
    
    
        public:
            room(int x, bool y);
            ~room(void);
            double getBill(int x, int y);
            int getNights(int x);
            int setNights(int x);
            int getOccNum(int y);
            int setOccNum(int y);
        };
    };
    Here is the implementation as I have it so far.
    Code:
    #include "hotel.h"#include <string>
    
    
    hotel::hotel(void)
    {
    }
    
    
    hotel::~hotel(void)
    {
    }
    
    
    hotel::customer(string x, string y)
    {
        name = x;
        CCNum = y;
    }
    
    
    
    
    hotel::~customer(void)
    {
    }
    
    
    hotel::room(int x, bool y)
    {
        RNum = x;
        isSmoking = y;
    }
    
    
    hotel::~room(void)
    {
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    You are miss-scoping your room constructor, room is not part of the hotel class but it's own class:

    Code:
    room::room(int x, bool y)
    {
        RNum = x;
        isSmoking = y;
    }
    Also you should start using initialization lists:

    Code:
    room::room(int x, bool y) : RNum(x), isSmoking(y) , bill(0.0), occNum(0), nights(0)
    {
       // Blank body.
    }
    Jim

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    since it is nested class it would be

    Code:
    hotel::room::room(int x, bool y):RNum(x),isSmoking(y), bill(0.0), occNum(0), nights(0)
    {
    }
    Also in C++ you do not need to add void for empty parameter list

    And destructor cannot have parameters

    P.S. parameter names like x and y are not very descriptive - your function will not be usable without documentation
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    1. Your class header needs to include the <string> header
    2. You then need to reference std::string instead of just string within said header... avoid putting a using namespace std statement in the header (at least at file scope).
    3. Your "get" functions should probably return a value and not rely on a passed in argument. You're doing it wrong anyway if that's what you do intend, the arguments would need to be passed by non-const reference.
    4. The "get" member functions should be declared as const member functions.
    5. The std::string objects being passed around should be passed as const reference parameters unless you intend to modify the arguments.
    6. As hinted at, you should prefer initialization lists in your constructors... in fact, you will be required to use them to initialize your const data members.
    Elysia likes this.
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested class
    By auralius in forum C++ Programming
    Replies: 4
    Last Post: 04-26-2012, 11:39 AM
  2. Can Nested class access parent class's private member?
    By meili100 in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2009, 09:42 AM
  3. Vector Construction problem..
    By SSJMetroid in forum C++ Programming
    Replies: 13
    Last Post: 10-11-2006, 06:50 PM
  4. Problem constructing a class with a nested struc
    By pliang in forum C++ Programming
    Replies: 3
    Last Post: 04-14-2005, 08:43 PM
  5. Problem with overloading and class construction
    By LouB in forum C++ Programming
    Replies: 18
    Last Post: 07-13-2002, 06:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21