Thread: Accessing my class from a queue

  1. #1
    Registered User immy's Avatar
    Join Date
    Apr 2014
    Posts
    33

    Accessing my class from a queue

    Hi guys the question I am attempting is
    The elements stored in a Queue object can be of any type, including other objects.
    Write an application to simulate a queue of people going to the cinema. The user may
    input "j" to join the queue, "l" to leave the queue or "e" to end the program.
    When a person joins the queue, their name and age is input and stored in a queue of
    Person objects.
    This evening, the cinema is showing a film rated as 15. When a person leaves the front
    of the queue, their age is checked and one of the following messages is output:
     <name> has left the queue and has entered the cinema
     <name> has left the queue, but is too young to enter the cinema

    Code:
    //Task 2: Cinema
    #include <iostream>
    #include "Person.h"
    #include <string>
    #include <queue>
    using namespace std;
    int main()
    {
        queue <Person>* myQueue;
        string e;
    
    
        cout << "<j> Join queue, <i> Leave queue, <end> Exit program";
        do
        {
            cout << "\n\nEnter a command: ";
            getline(cin, e);
            if (e == "j")
            {
                myQueue->push()
            }
    
    
            else if (e == "i")
            {
        
            }
    
    
            else
            {
                cout << "Incorrect entry. Please try again!\n";
            }
    
    
        } while (e != "end");
    
    
    
    
        system("PAUSE");
        return 0;
    }

    My problem is that on line 20 I cannot figure out how to access the queue from the person class and access the person methods
    Code:
    myQueue->push(/*Access Class*/)





    //Person c.pp
    Code:
    #include "Person.h"
    
    
    
    
    
    
    Person::Person()
    {
    }
    
    
    
    
    Person::~Person()
    {
    }
    
    
    void Person::setName(string n)
    {
        name = n;
    }
    
    
    string Person::GetName()
    {
        return name;
    }
    
    
    void Person::setAge(int a)
    {
        age = a;
    }
    
    
    int Person::GetAge()
    {
        return age;
    }
    Last edited by immy; 01-10-2017 at 02:00 PM.
    "Don't quit. Suffer now and live the rest of your life as a champion"
    - Muhammad Ali


  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Why are you using a pointer instead of a "normal" instance?

    Code:
    std::queue<Person> people;
    On line 20 you need to "push" something, not an empty pair of parentheses. It might be easier if you had a constructor that takes the proper parameters to initialize your class variables.

    Jim

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Let's take an example. Say you would input ages and store them into a vector. I'm sure you know how to do this. For example:

    std::vector<int> v;
    int age;
    // Input age
    v.push_back(age);

    Now simply generalize that to a person (i.e. instead of an age, you would have a Person). How would you do it? Can you think of a way?
    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.

  4. #4
    Registered User immy's Avatar
    Join Date
    Apr 2014
    Posts
    33
    Thanks for the replies, I realise that I need a local variable to access the class
    "Don't quit. Suffer now and live the rest of your life as a champion"
    - Muhammad Ali


  5. #5
    Registered User immy's Avatar
    Join Date
    Apr 2014
    Posts
    33
    I haven't learnt about vectors yet, thats the next chapter I need to learn.

    However I tried the code below and am getting a "no instance of overloaded function" error

    Code:
    //Task 2: Cinema
    #include <iostream>
    #include "Person.h"
    #include <string>
    #include <queue>
    using namespace std;
    int main()
    {
    	queue <Person>* myQueue;
    	Person cinema;
    	string e;
    
    
    	cout << "<j> Join queue, <i> Leave queue, <end> Exit program";
    	do
    	{
    		cout << "\n\nEnter a command: ";
    		getline(cin, e);
    		if (e == "j")
    		{
    			int a = 0;
    			string n;
    			cout << "Enter your age: ";
    			cin >> a;
    			cin.ignore();
    			myQueue->push(cinema.setAge(a));
    		}
    
    
    		else if (e == "i")
    		{
    	
    		}
    
    
    		else
    		{
    			cout << "Incorrect entry. Please try again!\n";
    		}
    
    
    	} while (e != "end");
    
    
    
    
    	system("PAUSE");
    	return 0;
    }
    "Don't quit. Suffer now and live the rest of your life as a champion"
    - Muhammad Ali


  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Whether you've learnt to use vectors or not, don't use a pointer.

    >>cinema.setAge(a)
    What do you think this does?
    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.

  7. #7
    Registered User immy's Avatar
    Join Date
    Apr 2014
    Posts
    33
    adds the users age to the queue
    "Don't quit. Suffer now and live the rest of your life as a champion"
    - Muhammad Ali


  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You realize that makes no sense? If you have a queue of people, then you can't come and give me an age. You can only give me a Person.

    My point is that you can only do

    Person p(...);
    // ...
    q.push(p);

    Because the it's a queue of "Person", so you have to put in an actual Person object.
    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.

  9. #9
    Registered User immy's Avatar
    Join Date
    Apr 2014
    Posts
    33
    Oh yes, I understand what your saying and that makes sense.

    Code:
    //Task 2: Cinema
    #include <iostream>
    #include "Person.h"
    #include <string>
    #include <queue>
    using namespace std;
    int main()
    {
    	queue <Person> myQueue;
    	Person cinema;
    	string e;
    
    
    	cout << "<j> Join queue, <i> Leave queue, <end> Exit program";
    	do
    	{
    		cout << "\n\nEnter a command: ";
    		getline(cin, e);
    		if (e == "j")
    		{
    			int a = 0;
    			string n;
    			cout << "Enter your name: ";
    			getline(cin, n);
    			cinema.setName(n);
    			cout << "Enter your age: ";
    			cin >> a;
    			cin.ignore();
    			cinema.setAge(a);
    			myQueue.push(cinema);
    		}
    
    
    		else if (e == "i")
    		{
    			myQueue.pop();
    		}
    
    
    		else
    		{
    			cout << "Incorrect entry. Please try again!\n";
    		}
    
    
    	} while (e != "end");
    
    
    	while (!myQueue.empty())
    	{
    		cout << endl << myQueue.front();
    		myQueue.pop();
    	}
    
    
    	system("PAUSE");
    	return 0;
    }
    I made some improvements, but I am struggling to output whats in the Queue.
    I get this error

    Severity Code Description Project File LineError C2679 binary '<<': no operator found which takes a right-hand operand of type 'Person' (or there is no acceptable conversion) 11t j:\private work\javatoc++\week 11\11t\11t\source.cpp 95
    "Don't quit. Suffer now and live the rest of your life as a champion"
    - Muhammad Ali


  10. #10
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    C++ is a boss language because it supports operator overloading which is basically a fancy way of saying, C++ (as a language) defines a set of operators. You can then use these operators with any arguments/data types that you desire. However, you have to define the nature of the operation itself.

    In your case, it's the:
    Code:
    cout << endl << myQueue.front();
    call that requires a definition.

    Try something like:
    Code:
    ostream & operator<<(ostream& os, Person const &p)
    {
      os << p.name << ", age: " << p.age;
      return os;
    }

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The compiler does not understand how to print a Person. You're either going to have to help it by creating a function that tells it how to do it (lookup operator overloading) or you can just create some code that prints the name and age and whatever else info you want.

    Also, it makes no sense to "reuse" a person, does it? You're effectively "creating" a new person and putting it into the queue when joining a person into the queue, right? So there's no reason to put the Person object outside the if statement. Put variables as close to use as possible!
    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
    May 2010
    Posts
    4,633
    I made some improvements, but I am struggling to output whats in the Queue.
    You either need to tell the program how to print a Person by overloading the output operator<< or print each member of the class explicitly.



    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 03-11-2016, 01:15 AM
  2. Replies: 11
    Last Post: 01-31-2016, 01:57 PM
  3. Accessing next entry in a queue.
    By peripatein in forum C Programming
    Replies: 84
    Last Post: 06-17-2013, 08:07 PM
  4. (queue*)this)->queue::qput’ does not have class type
    By brack in forum C++ Programming
    Replies: 13
    Last Post: 11-11-2010, 03:41 PM
  5. Base-class pointer, accessing object from derived class
    By Korhedron in forum C++ Programming
    Replies: 15
    Last Post: 09-28-2008, 05:30 AM

Tags for this Thread