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 } | | | | re: inserting and popping an element from a queue.
~Gee wrote:[color=blue]
> 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[/color]
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. | | | | re: inserting and popping an element from a queue.
"~Gee" <hungryforc@hotmail.com> wrote in message
news:83c2e7de.0405202254.7d9613f8@posting.google.c om...[color=blue]
> 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.
>[/color]
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
[color=blue]
> 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;[/color]
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.
[color=blue]
> 38 }
> 39 };
> 40
> 41 int main()
> 42 {
> 43 cout << "begin"<<endl;
> 44 display* d = new display();[/color]
Again a spurious use of new
[color=blue]
> 45 d->addToQ();
> 46
> 47 testClass* returnedObj = Q.pop();[/color]
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.
[color=blue]
> 48 cout << "size: " << Q.size() << endl;
> 49 returnedObj->printVals();
> 50 delete d;
> 51
> 52 return 0;
> 53 }[/color]
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 | | | | re: inserting and popping an element from a queue.
On Fri, 21 May 2004 09:28:46 +0100, "John Harrison"
<john_andronicus@hotmail.com> wrote:
[color=blue]
>
>"~Gee" <hungryforc@hotmail.com> wrote in message
>news:83c2e7de.0405202254.7d9613f8@posting.google. com...[color=green]
>> 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.
>>[/color]
>
>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
>[color=green]
>> 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;[/color]
>
>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.
>[color=green]
>> 38 }
>> 39 };
>> 40
>> 41 int main()
>> 42 {
>> 43 cout << "begin"<<endl;
>> 44 display* d = new display();[/color]
>
>Again a spurious use of new
>[color=green]
>> 45 d->addToQ();
>> 46
>> 47 testClass* returnedObj = Q.pop();[/color]
>
>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.
>[color=green]
>> 48 cout << "size: " << Q.size() << endl;
>> 49 returnedObj->printVals();
>> 50 delete d;
>> 51
>> 52 return 0;
>> 53 }[/color]
>
>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[/color]
A stack has S.top(), a queue has Q.front()
Remove brain, wash, rinse, spin, reinsert. :)
rossum
[color=blue]
> Q.pop();
> cout << "size: " << Q.size() << endl;
> returnedObj.printVals();
> return 0;
>}
>
>Are you a former Java programmer?
>
>john
>[/color]
--
The Ultimate Truth is that there is no Ultimate Truth | | | | re: inserting and popping an element from a queue.
> >[color=blue][color=green]
> >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[/color]
> A stack has S.top(), a queue has Q.front()
> Remove brain, wash, rinse, spin, reinsert. :)
>
> rossum
>[/color]
Yes apologies, and I had even looked it up to make sure.
john |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,272 network members.
|