1

I'm having a problem printing out the contents of an array used in a queue.

A portion of my template queue:

#include <iostream>
#include <cstdlib>
using namespace std;

template<class T>
class Queue
{
private:
    int front;      //front position
    int rear;       //rear position
    int maxQue;     //maximum number of elements in the queue
    T* items;       //points to a dynamically-allocated array code here
public:
    Queue()  // default constructor: Queue is created and empty
    {
        front = -1;
        rear = 0;
        maxQue = 10;
        items = new T[maxQue];
    }

    void Print()   // print the value of all elements in the queue
    {
        while(front != rear)
        {
            cout<<items[front];
            front++;
            if(front==rear)
               break;
            cout<<" - ";
        }
        cout<<endl;
    }

    void Enqueue(T add)      // insert x to the rear of the queue
    {                           // Precondition: the queue is not full
        if(IsFull())
        {
             cout<<"Queue is full!"<<endl;
        }
        else
        {
             items[rear] = add;
             rear++;
             rear = rear % maxQue;
        }
    }

    void Dequeue(T &x)  // delete the element from the front of the queue
    {                       // Precondition: the queue is not empty
         if(!IsEmpty())
         {
             front = (front+1)%maxQue;
             x = items[front];
         }
    }

    bool IsEmpty()   // test if the queue is empty
    {
         return (rear==front);
    } 

    bool IsFull()   // test if the queue is full
    {
         return ((rear+1)%maxQue==front);
    }

    int length()    // return the number of elements in the queue
    {
         return abs(rear-front);
    }

    ~Queue()  // Destructor:  memory for the dynamic array needs to be deallocated
    {
         delete [] items;
    }
};

A portion of the main routine:

int main()
{
     Queue<float>FloatQueue;
     float y;
     FloatQueue.MakeEmpty();

     FloatQueue.Dequeue(y);
     FloatQueue.Enqueue(7.1);
     cout << "float length 3 = " << FloatQueue.length() << endl;

     FloatQueue.Enqueue(2.3);
     cout << "float length 4 = " << FloatQueue.length() << endl;

     FloatQueue.Enqueue(3.1);
     FloatQueue.Dequeue(y);
     cout << "The float queue contains:  ";
     FloatQueue.Print();

     return 0;
}

The code compiles fine up until it tries to print, at which point I get these errors.

0 00000000  0x00466a7f in std::__convert_from_v() (??:??)  
1 00000000  0x00430302 in std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::_M_insert_float<double>() (??:??)  
2 00000000  0x00430da8 in std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put() (??:??)  
3 00000000  0x00447455 in std::ostream::_M_insert<double>() (??:??)  
4 00000000  0x00448988 in std::ostream::operator<<() (??:??)  
5 0041CB37  Queue<float>::Print(this=0x28ff00)

I've been stuck on this for a few days now, any help would be greatly appreciated.

Richard A
  • 21
  • 4

1 Answers1

0

It looks like you're implementing a fixed-size circular buffer. If so (or even if not) you have a few issues:

  1. If you enqueue more than the maximum size of the queue before taking anything out of it, it will never register as full.
  2. If your "front" pointer is greater than your rear pointer, your print function will never stop, and front will continue until MAX_INT and maybe loop around again. You're not doing the mod operation on the maximum size of the buffer.
  3. You have no destructor, so your buffer will leak every time you make and destroy one of these objects.
  4. Your length function is incorrect. Any time front is larger than rear (which is half the time) it will be in error. Think of it this way, when it's full, size will say zero.

And maybe a few other things than that. I'd re-think your design. You're close, but you have a few math errors.

Kevin Anderson
  • 6,850
  • 4
  • 32
  • 54