help with overloading operator= for a queue

Here is my code 4 the overloading of operator=
1
2
3
4
5
6
7
8
9
10
11
12
template<class Type>
constlinkedQueueType<Type>&linkedQueueType<Type>::operator=
                    (const linkedQueueType<Type>& otherQueue)
    nodeType<Type> * temp = otherQueue.queueFront;
    while(temp->link != NULL)
   {
      queueFront->info = temp->info;
      queueFront->link = new nodeType<Type>;
      queueFront = queueFront->link;
      temp = temp->link;
    }
}

Here is my main program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include"linkedQueue.h"
#include<queue>

using namespace std;

int main()
{
     linkedQueueType<int>myQueue;
     linkedQueueType<int> yourQueue;
     myQueue.addQueue(1732);
     myQueue.addQueue(1767);
     myQueue.addQueue(1734);
     myQueue.addQueue(1764);
     myQueue.addQueue(1766);

     //yourQueue = myQueue;
     while(!myQueue.isEmptyQueue())
     (
          cout<<myQueue.front() <<endl;
          myQueue.deleteQueue();
      }
}


With the line :
 
yourQueue = myQueue;

commented out.
Here is what I get:
1
2
3
4
5
6
7
8
1732
1767
1734
1764
1766

Process returned 0 (0x0)   execution time : 5.366 s
Press any key to continue.


Correct output.
Now when I uncomment the line
1
2
  yourQueue = myQueue;
 


The program bombs out.
which gives me the fact that there is something wrong with my operator= function.

Please help.





I'd recommend using a debugger to examine exactly what's going on when the crash occurs.
One point of undefined behavior is that you aren't returning anything from your operator=. That might not be the only issue though.

Another possible crash could result if otherQueue.queueFront is NULL (see lines 4-5 of operator=).
Last edited on
adding to Ganado's points
suppose `otherQueue' has only one element, `temp->link' will be null, never enter the loop and you don't copy the element.
now consider the last iteration of the loop, ¿is the queue null terminated? ¿where does `queueFront' point to?

you have an .addQueue() function, ¿why don't you use it?
1
2
traverse(element, otherQueue):
   this->addQueue(element)

edit: ¿do you have a queueBack pointer? (¿and why do you put `queue' to each member? ¿what else may be?)
Last edited on
Problem sorted out.
Thanks everybody
Found the answer from the book of Savitch
Topic archived. No new replies allowed.