468,457 Members | 1,764 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

inserting and popping an element from a queue.

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 }
Jul 22 '05 #1
4 4607
~Gee wrote:
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

--
Karthik.
Humans please 'removeme_' for my real email.
Jul 22 '05 #2

"~Gee" <hu********@hotmail.com> wrote in message
news:83**************************@posting.google.c om...
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
Jul 22 '05 #3
On Fri, 21 May 2004 09:28:46 +0100, "John Harrison"
<jo*************@hotmail.com> wrote:

"~Gee" <hu********@hotmail.com> wrote in message
news:83**************************@posting.google. com...
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

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

rossum
Q.pop();
cout << "size: " << Q.size() << endl;
returnedObj.printVals();
return 0;
}

Are you a former Java programmer?

john


--

The Ultimate Truth is that there is no Ultimate Truth
Jul 22 '05 #4
> >
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


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

john
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by JC | last post: by
5 posts views Thread by Russell Warren | last post: by
2 posts views Thread by sg71.cherub | last post: by
3 posts views Thread by thomas | last post: by
5 posts views Thread by randysimes | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.