Accessing my class from a queue

Jan 10, 2017 at 7:47pm
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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//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
 
myQueue->push(/*Access Class*/)




//Person c.pp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#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 on Jan 10, 2017 at 8:02pm
Jan 10, 2017 at 8:52pm
You need a local variable of type Person to work with.
Jan 10, 2017 at 9:16pm
Thanks
Jan 10, 2017 at 10:59pm
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//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;
}

Last edited on Jan 10, 2017 at 10:59pm
Jan 11, 2017 at 4:21am
myQueue is a pointer to a container, not a container of pointers, so try something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>
#include <queue>

class Person
{
    private:
        std::string name;
        int age;
    public:
        Person(const std::string& tmp_name, const int& tmp_age)
            : name(tmp_name), age(tmp_age){}
        ~Person(){}
        void setName(const std::string n);
        std::string GetName()const;
        void setAge(const int& a);
        int GetAge()const;
};

int main()
{
    std::queue <Person> myObjectQueue;
    std::queue <Person>* myQueue = &myObjectQueue;

    Person* tmp = new Person(std::string("John Smith"), 42);

    myQueue -> push(*tmp);
    std::cout << myQueue->front().GetName() << " " << myQueue->front().GetAge() << '\n';
    delete tmp;
}

void Person::setName(const std::string n)
{
	name = n;
}
std::string Person::GetName()const
{
	return name;
}
void Person::setAge(const int& a)
{
	age = a;
}
int Person::GetAge()const
{
	return age;
}

Output
 
John Smith 42 


Last edited on Jan 11, 2017 at 4:24am
Jan 11, 2017 at 11:02am
An alternative heap-created container:
1
2
3
4
5
6
7
8
int main()
{
    std::queue <Person>* myQueue = new std::queue<Person>;

    myQueue -> push(Person(std::string("John Smith"), 42));
    std::cout << myQueue->front().GetName() << " " << myQueue->front().GetAge() << '\n';
    delete myQueue;
}


edit: Program becomes even shorter with smart pointers: //include <memory>
1
2
3
4
5
6
int main()
{
    std::unique_ptr<std::queue <Person>> myQueue (new std::queue<Person>);
    myQueue -> push(Person(std::string("John Smith"), 42));
    std::cout << myQueue->front().GetName() << " " << myQueue->front().GetAge() << '\n';
}
Last edited on Jan 11, 2017 at 12:47pm
Jan 11, 2017 at 4:06pm
Thanks for the response, everything is working fine! I am quite intrigued on smart pointers (as I have never seen anything like that before) I assume it clears up the pointer memory automatically when the program ends.

Would you recommend any reading material with good easy explanations for beginners on smart pointers?
Jan 11, 2017 at 4:15pm
Jan 11, 2017 at 4:35pm
Cheers,
Topic archived. No new replies allowed.