queue tests not working

When I test the back function of my program I get a failure text from my driver program. My driver program also says the back of my queue is same as the front.
I think it might be a problem with my assignment operator or push function. Any ideas on what I did wrong in my queue header file?

Here is the driver code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

 cout << "Testing back()\n\n";

   int val1, val2;

   q6 = q7;

   val1 = q6.back();
   val2 = q7.back();
   val1 = q6.back();     // Make sure that back() doesn't remove a value.

   cout << ((val1 == val2) ? "back() works\n\n" : "back() failure\n\n");
   //failure 

   cout << "Testing front()\n\n";

   val1 = q6.front();
   val2 = q7.front();
   val1 = q6.front();    // Make sure that front() doesn't remove a value.

   cout << ((val1 == val2) ? "front() works\n\n" : "front() failure\n\n");
   //works 

   cout << "Testing const correctness\n\n";

   q7.clear();
   const Queue<char>& r6 = q6;

   cout << "q6 (size " << r6.size() << "): " << r6 << endl;
   cout << "q6 is " << ((r6.empty()) ? "empty\n" : "not empty\n");
   cout << "Front item of q6: " << r6.front() << endl;
   cout << "Back item of q6: " << r6.back() << endl << endl; //this prints the 
                                                           //same as the front

Header code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#ifndef QUEUE_H
#define QUEUE_H

#include <iostream>
#include <stdexcept>
#include <cstdlib>

using std::out_of_range;

template <class T>
struct QNode
   {
     QNode(const T&);

     //Data members
     T data;
     QNode<T> *next;
   };


//Method definitions for the QNode class

template <class T>
QNode<T>::QNode(const T& newData)
{
   data = newData;
   next = NULL;
}



// Forward declaration of the Queue template class
template <class T>
std::ostream& operator<<(std::ostream&, const Queue<T>&);

template <class T>
class Queue
   {
   // friend declaration for the template function - note the
   // special syntax
   friend std::ostream& operator<< <>(std::ostream&, const Queue<T>&);

   public:

        Queue();
       ~Queue();
        Queue(const Queue<T>&);
        int size() const;
        bool empty() const;
        void clear();
        T front() const throw(out_of_range);
        T back() const throw(out_of_range);
        void push(const T&);
        void pop() throw(out_of_range);


    //Operators to overload
      Queue<T>& operator=(const Queue<T>&);

   private:

         QNode<T> *qFront;
         QNode<T> *qRear;

   };

// Method definitions for the Queue class

template <class T>
std::ostream& operator<<(std::ostream& out, const Queue<T>& rightOp)
{
  QNode<T> *ptr;
  ptr = rightOp.qFront;

  while(ptr != NULL)
  {
     out << " " << ptr->data;
     ptr = ptr->next;
  }


  return out;
}

template <class T>
Queue<T>::Queue()
{
  qFront = NULL;
  qRear  = NULL;
}

template <class T>
int Queue<T>::size() const
{
  int count = 0;
  QNode<T> *ptr;

  ptr = qFront;

  while(ptr != NULL)
  {
   count++;
   ptr = ptr->next;
  }

 return count;

}

template <class T>
bool Queue<T>::empty() const
{
  if (qFront == NULL)
     return true;
  else
     return false;
}

template <class T>
void Queue<T>::push(const T& item)
{
  QNode<T> *ptr;
  ptr = new QNode<T>(item);

  ptr->data = item;
  ptr->next = NULL;

  if(qFront == NULL)
  {
    qFront = ptr;
  }
  else
  {
    qRear->next = ptr;
  }
  qRear = ptr;
}

template <class T>
Queue<T>::Queue(const Queue<T>& copy)
{

  QNode<T> *ptr, *lagPtr;
  if(copy.qFront == NULL)
  {
    qFront = NULL;
  }
  else
  {
      qFront = new QNode<T>(copy.qFront->data);
      ptr = copy.qFront->next;
      lagPtr = qFront;
                  
 while(ptr != NULL)
      {
        lagPtr->next = new QNode<T>(ptr->data);
        lagPtr = lagPtr->next;
        ptr = ptr->next;
      }
    lagPtr->next = NULL;
  }
}

template <class T>
Queue<T>::~Queue()
{
  clear();
}

template <class T>
void Queue<T>::clear()
{
  QNode<T> *ptr, *next;
  ptr = qFront;

  while(ptr != NULL)
  {
    next = ptr->next;
    delete next;
    ptr = next;
  }

  qFront = NULL;
  qRear  = NULL;
}

template <class T>
Queue<T>& Queue<T>::operator=(const Queue<T>& rightOp)
{
  QNode<T> *ptr, *lagPtr;

  if(this != &rightOp)
  {

    delete qFront;
      qFront = new QNode<T>(rightOp.qFront->data);
      ptr = rightOp.qFront->next;
      lagPtr = qFront;

      while(ptr != NULL)
      {
        lagPtr->next = new QNode<T>(ptr->data);
        lagPtr = lagPtr->next;
        ptr = ptr->next;
      }
    lagPtr->next = NULL;
  }

return *this;

}

template <class T>
void Queue<T>::pop()  throw(out_of_range)
{
  QNode<T> *ptr;

  if(qFront == NULL)
    throw out_of_range("Queue underflow on pop()");
  else
  {
   ptr = qFront;
   qFront = qFront->next;
   delete ptr;
  }

}

template <class T>
T Queue<T>::front() const throw(out_of_range)
{
  if(qFront == NULL)
    throw out_of_range("Queue underflow on front()");
  else
    return qFront->data;

}

template <class T>
T Queue<T>::back() const throw(out_of_range)
{
  if(qFront == NULL)
    throw out_of_range("Queue underflow on back()");
  else
   return qRear->data;
}

#endif /* QUEUE_H */ 
closed account (D80DSL3A)
qRear is not assigned a value in your operator=(). A good place for that would be after line 206:
qRear = lagPtr;

Other problems in this function:
1) Line 195 delete qFront;. Don't you want to call clear() here?
2) Check that rightOp.qFront != NULL before line 196.

An error in your clear():
Line 179 should be delete ptr;

Some other details:
1) Why check if(qFront == NULL) in the back()? Check if(qBack == NULL)
2) If pop() pops the last node you will get qFront = NULL (correctly) but qRear needs to be assigned = NULL too.

I have not looked at everything. There may be other issues.
Hope this helps.
Oh wow I completely overlooked the qRear = lagPtr in the assignment operator= . All those changes corrected the issues I had. Thanks a lot.
Topic archived. No new replies allowed.