Like Tree2Likes

constant integer class variable errors

This is a discussion on constant integer class variable errors within the C++ Programming forums, part of the General Programming Boards category; The purpose of the program I am writing is to write a basic hotel, room, and customer nested class example. ...

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

    constant integer class variable errors

    The purpose of the program I am writing is to write a basic hotel, room, and customer nested class example. Currently, my logic is that I initialize an array of rooms inside of Class Hotel. The problem is, since the teacher wants our array to be of variable MAX_SIZE spaces, I have to initialize a constant integer MAX_SIZE, but it turns up all of these errors that I don't know how to fix.

    The code is below. If you need me to post extra code, like the room class header, etc. (you shouldn't, as I've already eliminated all of the other errors in the code), let me know and I'll post.

    Below is Hotel.h

    Code:
    #include "Room.h"
    
    #pragma once
    class Hotel
    {
    	int hotelID;
    
    
    	// Class Room will not compile unless publically accessible.
    public:
    	Hotel(void);
    	~Hotel(void);
    	const int MAX_SIZE = 101;
    	Room room[MAX_SIZE];
    };
    Below is Hotel.cpp

    Code:
    #include "Hotel.h"#include "Room.cpp"
    
    
    Hotel::Hotel(void)
    {
    }
    
    
    Hotel::~Hotel(void)
    {
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,899
    but it turns up all of these errors that I don't know how to fix.
    What are your errors?

    Jim

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Code:
    const int MAX_SIZE = 101
    This is a const-qualified member variable (not a true constant). You can achieve your goal by making it "static const int...", but I would encourage you to use std::vector instead.
    vart likes this.
    I never put signature, but I decided to make an exception.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    68
    Thanks, that fixed the problem with MAX_SIZE.

    Now, however, simply due to the way I designed the program, I'm getting all sorts of link errors (function x is already defined in y.h file). How would I go about fixing this?

    Hotel.h includes Room.h
    Hotel.cpp includes Hotel.h and Room.cpp
    Room.h includes Customer.h
    Room.cpp includes Room.h and Customer.cpp
    Customer.h includes string
    Customer.cpp inludes Customer.h and <iostream>

    I've narrowed down the problems to something about the nesting, as a hotel member is room, which has a member class customer. The errors I have now are things like "function x::y already defined," but we have to include them in separate .h and .cpp files. Is there a way to fix this? If you need me to, I'll post the exact code of all.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    .cpp files should not include other .cpp files. Remove the inclusion of .cpp files and see what happens.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    68
    Slightly fewer errors. Also forgot to add.

    Main includes Hotel.h, Hotel.cpp, Room.cpp, and Customer.cpp now.

    EDIT: Strike that. Removed inclusions above (save Hotel.h). Now I have three unresolved external symbol errors (two of which are born from attempting to pass a string to a function). I'm working on that.
    Last edited by Trey Brumley; 09-30-2013 at 05:00 PM.

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    To resolve unresolved externals (it they are your functions defined in different files) - you need to add all your files (cpp+h) to the project being build...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    68
    It's added. Problem is, it's only one function in particular. Room::bool isSmoking(int n). I fixed the second problem, but this one remains.

    Code:
    bool isSmoking (int n){
    	if (n <= 20)
    		return true;
    	else
    		return false;
    }

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Quote Originally Posted by Trey Brumley View Post
    I fixed the second problem, but this one remains.
    What error?

    Btw, your code basically boils down to:
    Code:
    bool IsSmoking (int n) { return (n <= 20); }
    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
    Registered User
    Join Date
    Nov 2012
    Posts
    68
    Here are my errors.
    Code:
    1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Platforms\Win32\Microsoft.Cpp.Win32.Targets(268,5): error MSB6006: "link.exe" exited with code 1120.1>Main.obj : error LNK2019: unresolved external symbol "public: bool __thiscall Room::isSmoking(int)" (?isSmoking@Room@@QAE_NH@Z) referenced in function "void __cdecl roomStatus(class Room,int)" (?roomStatus@@YAXVRoom@@H@Z)
    1>C:\Users\Trey\documents\visual studio 2010\Projects\OOP Program 1\Debug\OOP Program 1.exe : fatal error LNK1120: 1 unresolved externals
    Here are my three files. Main is still in progress.

    room.h
    Code:
    #include "Customer.h"
    
    #pragma once
    	class Room
    	{
    	// Class customer and the data types below will not compile unless
    	// publically accessible.
    	public:
    		Room(void);
    		~Room(void);
    		Customer customer;
    		double bill;
    		int occNum;
    		int nights;
    
    
    	// Member functions, publically accessible.
    		bool isSmoking (int n);
    		double getBill(int nights, int occNum);
    		int getNights();
    		void setNights(int days);
    		int getOccNum();
    		void setOccNum(int ppl);
    		void assignCustomer(Customer c);
    		void clearRoom();
    	};
    room.cpp
    Code:
    #include "Room.h"
    
    Room::Room(void)
    {
    };
    
    
    Room::~Room(void)
    {
    };
    
    
    bool IsSmoking (int n)
    {
    	return (n <= 20);
    }
    
    
    double Room::getBill(int nights, int occNum)
    {
    	double money = 1.00;
    	int x = (occNum-1)*10;
    	int y = (60+occNum)*nights;
    	money = money*y;
    	return money;
    };
    
    
    int Room::getNights()
    {
    	return nights;
    };
    
    
    void Room::setNights(int days)
    {
    	nights = days;
    	return;
    };
    
    
    int Room::getOccNum()
    {
    	return occNum;
    };
    
    
    void Room::setOccNum(int ppl)
    {
    	occNum = ppl;
    	return;
    };
    
    
    void Room::assignCustomer(Customer c)
    {
    	customer.setName(c.getName());
    	customer.setCCNum(c.getCCNum());
    	return;
    };
    
    
    void Room::clearRoom()
    {
    	occNum = 0;
    	nights = 0;
    	customer.setName("");
    	customer.setCCNum("");
    	return;
    }
    main.cpp
    Code:
    // Trey Brumley// Shujing Zhang
    // 1 October 2013
    // Stringfellow - 2143
    // Project 1 - Hotel Program
    // =========================
    
    
    #include "Hotel.h"
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    void assigmSmoking(Room r, int n);
    void checkIn(Room r);
    void unoccupiedRooms(Hotel h);
    void roomStatus(Room r, int n);
    void modifyRoom(Room r);
    void hotelOccupancy(Hotel ho);
    void checkOut(Room r);
    
    
    int main (int argc, char *argv[])
    {
    	Hotel hotel;
    	char ch;
    	int roomNum;
    
    
    	cout << "Please enter one of the following letters to perform a function." << endl;
    	cout << "To check a customer into a room, enter C (for Check-in)." << endl;
    	cout << "To check how many rooms are occupied, enter O (for Occupancy)." << endl;
    	cout << "To check the status of a room, enter S (for Status)." << endl;
    	cout << "To modify room data (e.g. change nights, add or subtract occupants), enter M (for Modify)." << endl;
    	cout << "To find an open room (smoking and non will be indicated), enter F (for Find)." << endl;
    	cout << "To calculate a room's current bill, enter B (for Bill)." << endl;
    	cout << "To check a customer out of a room, enter E (for Exit)." << endl;
    	cout << "To quit this program, enter Q (for Quit)." << endl;
    	cout << "Please enter your command letter now:  ";
    	cin >> ch;
    	ch = toupper(ch);
    
    
    	while (ch != 'Q')
    	{
    		switch (ch)
    		{
    		case 'C':
    			cout << "Which room is being checked in to?  ";
    			cin >> roomNum;
    			checkIn(hotel.room[roomNum]);
    			break;
    		case 'O':
    			hotelOccupancy(hotel);
    			break;
    		case 'S':
    			cout << "Please enter a room number to check:  ";
    			cin >> roomNum;
    			roomStatus(hotel.room[roomNum], roomNum);
    			break;
    		case 'M':
    			cout << "Please enter a room number to modify:  ";
    			cin >> roomNum;
    			modifyRoom(hotel.room[roomNum]);
    			break;
    		case 'B':
    			cout << "Which room is being calculated?  ";
    			cin >> roomNum;
    			hotel.room[roomNum].getBill(hotel.room[roomNum].nights, hotel.room[roomNum].occNum);
    			break;
    		case 'E':
    			cout << "Which room is being checked out?  ";
    			cin >> roomNum;
    
    
    			break;
    		case 'F':
    			unoccupiedRooms(hotel);
    			break;
    		default:
    			cout << "ERROR:  Invalid operation."  << endl;
    			break;
    		}
    		cout << "Please enter a command (to review the list, return to the top of this list):  ";
    		cin >> ch;
    		ch = toupper(ch);
    	}
    
    
    	cout << "Thank-you for using this program!  Have a nice day!" << endl;
    	system("pause");
    	return 0;
    }
    
    
    void checkIn (Room r)
    {
    	string name, CCnum;
    	cout << "Please enter the name of the customer checking in:  ";
    	cin >> name;
    	cout << "Please enter the credit card number:  ";
    	cin >> CCnum;
    	r.customer.setCCNum(CCnum);
    	r.customer.setName(name);
    
    
    	return;
    }
    
    
    // Finds unoccupied one smoking and one nonsmoking room.
    void unoccupiedRooms(Hotel h)
    {
    	int s = 0;
    	int n = 0;
    	int x = 1;
    
    
    	//Checks all smoking rooms, finds the lowest-numbered unoccupied room, and
    	//prints the result.
    	while (s == 0 && x <= 20)
    	{
    		if (h.room[x].occNum == 0)
    			x++;
    		else
    			s = x;
    	}
    
    
    	if (s == 0)
    		cout << "There are no smoking rooms available." << endl;
    	else
    		cout << "Smoking Room " << s << " is available." << endl;
    
    
    	// Sets x to 21 and checks all non-smoking rooms the same.
    	x = 21;
    	while (n == 0 && x <= 100)
    	{
    		if (h.room[x].occNum == 0)
    			x++;
    		else
    			n = x;
    	}
    
    
    	if (n == 0)
    		cout << "There are no non-smoking rooms available." << endl;
    	else
    		cout << "Non-Smoking Room " << n << " is available." << endl;
    
    
    	return;
    }
    
    
    void roomStatus (Room r, int n)
    {
    	int x = r.getOccNum();
    	if (x != 0)
    	{
    		if (r.isSmoking(n))
    			cout << "Smoking:  YES." << endl;
    		else
    			cout << "Smoking:  NO." << endl;
    		cout << "Customer:  " << r.customer.getName() << endl;
    		cout << "Occupants:  " << r.getOccNum() << endl;
    		cout << "Nights occupied:  " << r.getNights() << endl;
    	}
    	else
    		cout << "This room is not occupied." << endl;
    
    
    	return;
    }
    void modifyRoom (Room r)
    {
    
    
    
    
    
    
    	return;
    }
    
    
    //Checks how many rooms are not occupied.
    void hotelOccupancy (Hotel ho)
    {
    	int i=0;
    	int j=0;
    	int x = 0;
    
    
    	//Checks all non-smoking rooms.  If a room has no occupants, the counter adds one.
    	//Otherwise, it goes to the next room.  At the end, it prints out its findings.
    	while (x <= 20)
    	{
    		if (ho.room[x].occNum == 0)
    			x++;
    		else
    			i++;
    			x++;
    	}
    
    
    	if (i == 0)
    		cout << "There are no smoking rooms available." << endl;
    	else if (i == 1)
    		cout << "There is " << i << " smoking room available." << endl;
    	else
    		cout << "There are " << i << " smoking rooms available." << endl;
    
    
    	//Sets x to 21 and checks all non-smoking rooms the same way.
    	x = 21;
    	while (x <= 100)
    	{
    		if (ho.room[x].occNum != 0)
    			x++;
    		else
    			j++;
    			x++;
    	}
    	if (j == 0)
    		cout << "There are no smoking rooms available." << endl;
    	else if (j == 1)
    		cout << "There is " << i << " smoking room available." << endl;
    	else
    		cout << "There are " << i << " smoking rooms available." << endl;
    
    
    	return;
    }
    
    
    void checkOut(Room r)
    {
    	r.customer.printReceipt(r.bill, r.customer.CCNum);
    	r.clearRoom();
    	return;
    }

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    There are two problems:
    - The first is that you declared the function as bool isSmoking (int n), but defined it as IsSmoking (notice the leading capital letter). Remember that C++ is case sensitive!
    - The second is that you declared it as a global function. You forgot to append Room:: to it so the compiler knows it's part of the Room class.
    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.

  12. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    68
    Thank-you. That problem is now fixed. Two more problems, because I have written another segment of main. In my modifyRoom function, I am getting "illegal case" at case 'O', and "illegal default." What's causing these? They aren't affecting my main function at all, or Case 'N' in the function. Is it because these are in my main function?

    Code:
    void modifyRoom (Room r){
    	int i = 0;
    	char c;
    	cout << "Enter N for Nights, O for number of Occupants, or Q to return to the main menu.";
    	cin >> c;
    	c = toupper(c);
    	
    	while (c != 'Q')
    	{
    	switch(c)
    		case 'N':
    			cout << "Please enter the number of nights stayed:  ";
    			cin >> i;
    			r.setNights(i);
    			break;
    		case 'O':
    			cout << "Please enter the number of occupants:  ";
    			cin >> i;
    			r.setOccNum(i);
    			break;
    		default:
    			cout << "ERROR:  Invalid answer.  Please enter again:  ";
    			cin >> c;
    			break;
    	}
    
    
    	return;
    }

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Switch statement requires braces:
    Code:
    switch (expr)
    {
    	case A:
    	case B:
    	// etc
    }
    You probably want to pass your room by const reference, also.
    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.

  14. #14
    Registered User
    Join Date
    Nov 2012
    Posts
    68
    All these problems are getting fixed one at a time, but every time they get fixed, a new one unrelated appears. Fortunately, they are becoming less and less in number. Thanks for all the help.

    My latest one is in the following segment. I need to read in an integer (room number), a string (name), and another string (CCNum) (separated by spaces and tabs), all from the same line in the text file. Here was my original idea (which is wrong). How does this actually work?

    Code:
    int rn;
    string Name, ccnum;
    
    ifstream infile;    
    infile.open("input.txt");
        if (infile.is_open)
        {
            while (infile.eof() == false)
            {
                Customer c;
                infile.get(rn);
                c.setName(infile.get(Name));
                c.setCCNum(infile.get(ccnum));
                hotel.room[rn].assignCustomer(c);
            }
            infile.close();
        }
        else
            cout << "ERROR:  Unable to open text file." << endl;

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    >>ifstream infile;
    >>infile.open("input.txt");
    Simplify to
    ifstream infile("input.txt");

    >>if (infile.is_open)
    Don't forget parenthesises!
    if (infile.is_open())

    >>while (infile.eof() == false)
    More commonly written as while (!infile.eof()).
    However, this is wrong. See c++ - Reading from text file until EOF repeats last line - Stack Overflow.

    >>infile.get(rn);
    get() reads a character. You want to read an int. You can use the extraction operator (>>) for that.
    Eg: infile >> myint;
    The extraction works with most basic types (ints, std::string, etc). It stops reading at whitespace (spaces, tabs, etc). So long as you separate your strings and integers like that, then it will work fine.

    >>infile.close();
    Unnecessary. C++ closes files automatically.
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. integer constant is too large for 'long' type
    By rehman12390 in forum C++ Programming
    Replies: 2
    Last Post: 05-25-2012, 07:12 AM
  2. Replies: 5
    Last Post: 03-04-2012, 12:56 AM
  3. integer constant data types
    By password636 in forum C Programming
    Replies: 3
    Last Post: 11-22-2011, 09:38 AM
  4. integer constant is too large for type
    By Abda92 in forum C++ Programming
    Replies: 8
    Last Post: 02-09-2008, 11:47 AM
  5. integer to constant char w/o using itoa()
    By kes103 in forum C++ Programming
    Replies: 1
    Last Post: 01-06-2003, 10:42 PM

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