473,322 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 4820
~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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: JC | last post by:
Hello all, I was wondering if someone can give me some tips on this problem. I'm trying to take content of a queue (Xqueue) and copy it into another queue (Yqueue) I tried using a while...
7
by: Jared Evans | last post by:
I developed a console application that will continually check a message queue to watch for any incoming data that needs to be inserted into MS SQL database. What would be a low-cost method I...
2
by: worli | last post by:
Hi All, I have a strange requirement. I have a dynamic input numeric data stream e.g. 2, 2, 4, 5 etc.... ( each input number range from 1 to 10 ). lets take a simple case where all inputs will...
5
by: Russell Warren | last post by:
Does anyone have an easier/faster/better way of popping from the middle of a deque than this? class mydeque(deque): def popmiddle(self, pos): self.rotate(-pos) ret = self.popleft()...
12
by: desktop | last post by:
Why does insert only work when specifying an iterator plus the object to be inserted: std::vector<intt; std::vector<int>::iterator it; it = t.begin(); t.insert(it,33); If I use push_back...
1
by: madhuxml82 | last post by:
Dear Forum Members, I have generated an XML Schema and a Table of XMLType referencing the XML Schema. Now When I am Inserting the Data into the Table. I am getting the Error 0RA-30937: Error is...
2
by: sg71.cherub | last post by:
HI all, I use <queueof STL. Before pushing an element into the queue, how to check whether the queue has contained the element? i.e. I want the queue has no duplicated elements. Is ther...
3
by: thomas | last post by:
I want to use a priority_queue like STL data structure. But I found that priority_queue cannot update element value: only pop/ push is supported. I'm using priority_queue to implement the prim...
5
by: randysimes | last post by:
I have to make a queue utilizing a linked list for an assignment. When I Push() an item to the queue, and try to Pop() or return the Front() element, it says the queue is empty. When I Push() another...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.