title: What is wrong with my Copy Constructor?
body: <p>Why does it crash when I get to the copy constructor function?</p>
  
  <p>In the copy procedure that you will find in my class definitions, I'm making sure that the Queue that will be created as a copy of some other original Queue is empty before copying begins: let's say Queue q1 is not empty and I want to turn q1 into q2. I want to empty the contents of q1 before copying the contents of q2 to q1..</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;cassert&gt;
  
  using namespace std;
  
  class Dnode
  {
      public:
         Dnode(int);
         int  n;
         Dnode*   l, *r;
  };
  
  Dnode::Dnode(int tx)
  {
      n = tx;
      l = r = NULL;
  }
  
  class Queue // reminder: insertions at the rear, deletions at the front
  {
      public:
         Queue();
         void enqueue(int x);
         int dequeue(void);
         bool empty(void) const;
         void display(void) const;
         Queue(const Queue&amp;); //copy constructor
  
      private:
         Dnode*   front, *back;
         void copy(Dnode*);
         void free();
  
  };
  
  Queue::Queue()
  {
      front = back = NULL;
  }
  
  
  void Queue::enqueue(int x)
  {
      Dnode*  d = new Dnode(x);
      if (empty())
          front = back = d;
      else
      {
          back-&gt;r = d;
          d-&gt;l = back;
          back = d;
      }
  }
  
  int Queue::dequeue(void)
  {
      assert(! empty());
      Dnode*  temp = front;
      front = front-&gt;r;
      if (front == NULL)
          back = NULL;
      else    front-&gt;l = NULL;
      int x = temp-&gt;n;
      delete temp;
      return x;
  }
  
  
  bool Queue::empty(void) const
  {
      return front == NULL;
  }
  
  void Queue::display(void) const
  {
      for (Dnode* d = front; d != NULL; d = d-&gt;r)
          cout &lt;&lt; d-&gt;n &lt;&lt; " ";
      cout &lt;&lt; endl;
  }
  
  void Queue::copy(Dnode* dn) // "dn" will be "Front" of Queue being copied
  {                           // this procedure will be called in Copy Constructor  
      Dnode* temp=front;      // found underneath this procedure
      while(front!=back)      
      {                       
          front=front-&gt;r;
          delete temp;
          temp=front;
      }
  
      delete temp;
  
      front=back=temp=NULL;
  
      if(dn!=NULL)
      {
          while(dn-&gt;r!=NULL)
          {
              enqueue(dn-&gt;n);
              dn=dn-&gt;r;
          }
          enqueue(dn-&gt;n);
      }
  }
  
  Queue::Queue(const Queue&amp; x)
  {
      copy(x.front);
  }
  
  int main()
  {
      Queue   q;
      if (q.empty()) cout &lt;&lt; "q empty" &lt;&lt; endl;
  
      for (int i = 0; i &lt; 10; i++) q.enqueue(i);
  
      q.display();
  
      int x = q.dequeue();
  
      cout &lt;&lt; "x is " &lt;&lt; x &lt;&lt; endl;
  
      q.display();
  
      Queue q1(q); //&lt;----program crashes when we get here
  
      q1.display();
  }
  </code></pre>