inserting and popping an element from a queue.

G

~Gee

Hi,
When I try to compile the following program, I get the following
error:

$ g++ anotherTest.C
anotherTest.C: In function `int main()':
anotherTest.C:47: void value not ignored as it ought to be

What I am trying to do is that I have a global queue. I create an
object of class "testClass" and then add it to the queue. My question
is how do I pop this element from the queue. I am trying to pop the
element using line 47 but it is throwing an error. Any inputs would be
appreciated.

Thanks,
~Gee

1 #include <iostream>
2 #include <queue>
3 #include <list>
4
5 using namespace std;
6 class testClass;
7 class display;
8 std::queue<testClass> Q;
9
10 class testClass
11 {
12 public:
13 inline testClass(int a, int b, int c)
14 {
15 _a = a;
16 _b = b;
17 _c = c;
18 }
19
20 inline string printVals()
21 {
22 cout << "a: " << _a << " b: " << _b << " c: " << _c;
23 }
24
25 private:
26 int _a, _b,_c;
27 };
28
29 class display
30 {
31 public:
32
33 void addToQ()
34 {
35 testClass* obj = new testClass(1, 2, 3);
36 Q.push(*obj);
37 delete obj;
38 }
39 };
40
41 int main()
42 {
43 cout << "begin"<<endl;
44 display* d = new display();
45 d->addToQ();
46
47 testClass* returnedObj = Q.pop();
48 cout << "size: " << Q.size() << endl;
49 returnedObj->printVals();
50 delete d;
51
52 return 0;
53 }
 
K

Karthik

~Gee said:
Hi,
When I try to compile the following program, I get the following
error:

$ g++ anotherTest.C
anotherTest.C: In function `int main()':
anotherTest.C:47: void value not ignored as it ought to be

pop() method returns void. Hence the problem.

Try using front() method in the same. It returns a constant reference to
the object in the front of the queue.

HTH
 
J

John Harrison

~Gee said:
Hi,
When I try to compile the following program, I get the following
error:

$ g++ anotherTest.C
anotherTest.C: In function `int main()':
anotherTest.C:47: void value not ignored as it ought to be

What I am trying to do is that I have a global queue. I create an
object of class "testClass" and then add it to the queue. My question
is how do I pop this element from the queue. I am trying to pop the
element using line 47 but it is throwing an error. Any inputs would be
appreciated.

RTFM I think.

pop removes an element from the queue, it does not return that element. This
is a deliberate design decision because it is impossible to implement pop so
that it returns the removed element and gives strong exception safety
guarantees.

For other problems with your code see below
Thanks,
~Gee

1 #include <iostream>
2 #include <queue>
3 #include <list>
4
5 using namespace std;
6 class testClass;
7 class display;
8 std::queue<testClass> Q;
9
10 class testClass
11 {
12 public:
13 inline testClass(int a, int b, int c)
14 {
15 _a = a;
16 _b = b;
17 _c = c;
18 }
19
20 inline string printVals()
21 {
22 cout << "a: " << _a << " b: " << _b << " c: " << _c;
23 }
24
25 private:
26 int _a, _b,_c;
27 };
28
29 class display
30 {
31 public:
32
33 void addToQ()
34 {
35 testClass* obj = new testClass(1, 2, 3);
36 Q.push(*obj);
37 delete obj;

This is a spurious use of new, you should simply say

Q.push(testClass(1, 2, 3));

By avoiding new and delete you get more efficient, simpler and safer code.
See the recent thread 'simple delete question' started by cppaddict for
more information. Might clear up your confusion about pointers as well.
38 }
39 };
40
41 int main()
42 {
43 cout << "begin"<<endl;
44 display* d = new display();

Again a spurious use of new
45 d->addToQ();
46
47 testClass* returnedObj = Q.pop();

You aren't putting pointers on your queue, so I don't know why you think you
are going to get a pointer back. And as already mentioned pop returns void.
48 cout << "size: " << Q.size() << endl;
49 returnedObj->printVals();
50 delete d;
51
52 return 0;
53 }

Here main rewritten to avoid new and delete and to use pop correctly

int main()
{
cout << "begin"<<endl;
display d;
d.addToQ();
testClass returnedObj = Q.top(); // top returns the top object
Q.pop();
cout << "size: " << Q.size() << endl;
returnedObj.printVals();
return 0;
}

Are you a former Java programmer?

john
 
R

rossum

RTFM I think.

pop removes an element from the queue, it does not return that element. This
is a deliberate design decision because it is impossible to implement pop so
that it returns the removed element and gives strong exception safety
guarantees.

For other problems with your code see below


This is a spurious use of new, you should simply say

Q.push(testClass(1, 2, 3));

By avoiding new and delete you get more efficient, simpler and safer code.
See the recent thread 'simple delete question' started by cppaddict for
more information. Might clear up your confusion about pointers as well.


Again a spurious use of new


You aren't putting pointers on your queue, so I don't know why you think you
are going to get a pointer back. And as already mentioned pop returns void.


Here main rewritten to avoid new and delete and to use pop correctly

int main()
{
cout << "begin"<<endl;
display d;
d.addToQ();
testClass returnedObj = Q.top(); // top returns the top object
A stack has S.top(), a queue has Q.front()
Remove brain, wash, rinse, spin, reinsert. :)

rossum
 
J

John Harrison

A stack has S.top(), a queue has Q.front()
Remove brain, wash, rinse, spin, reinsert. :)

rossum

Yes apologies, and I had even looked it up to make sure.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top