inserting and popping an element from a queue.

Discussion in 'C++' started by ~Gee, May 21, 2004.

  1. ~Gee

    ~Gee Guest

    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 }
     
    ~Gee, May 21, 2004
    #1
    1. Advertising

  2. ~Gee

    Karthik Guest

    ~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.
     
    Karthik, May 21, 2004
    #2
    1. Advertising

  3. "~Gee" <> wrote in message
    news:...
    > 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
     
    John Harrison, May 21, 2004
    #3
  4. ~Gee

    rossum Guest

    On Fri, 21 May 2004 09:28:46 +0100, "John Harrison"
    <> wrote:

    >
    >"~Gee" <> wrote in message
    >news:...
    >> 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
     
    rossum, May 21, 2004
    #4
  5. > >
    > >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
     
    John Harrison, May 22, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Russell Warren

    Is Queue.Queue.queue.clear() thread-safe?

    Russell Warren, Jun 22, 2006, in forum: Python
    Replies:
    4
    Views:
    685
    Russell Warren
    Jun 27, 2006
  2. Kceiw
    Replies:
    3
    Views:
    1,001
    Jim Langston
    Mar 14, 2006
  3. Frank Millman
    Replies:
    3
    Views:
    349
    Frank Millman
    Feb 9, 2010
  4. Kris
    Replies:
    0
    Views:
    488
  5. bintom
    Replies:
    6
    Views:
    640
    Öö Tiib
    Nov 3, 2012
Loading...

Share This Page