title: Binary Tree - Copy Constructor
body: <p>I am trying to create a copy constructor for my Binary tree.</p>
  
  <p><strong>My problem:</strong></p>
  
  <p>I can see the values of the source tree getting copied into the target tree but when it comes to writing the values out, there are no values in the copied tree and it crash's my program.</p>
  
  <p><strong>Error Message:</strong></p>
  
  <p>Unhandled exception at 0x0097a43c in binTree.exe: 0xC0000005: Access violation reading location 0xccccccec.</p>
  
  <p><strong>Code:</strong></p>
  
  <p>// Main Method</p>
  
  <pre><code>    int main(int argc, char **) {
      ifstream fin6("input_data.txt");
      ofstream out9("copied_tree.txt");
  
      if(!fin6.is_open()) 
      {
          cout &lt;&lt; "FAIL" &lt;&lt; endl;
          return 1;
      }
  
      BinaryTreeStorage binaryTreeStorage2;
  
      // read in values into data structure
      binaryTreeStorage2.read(fin6);
  
      BinaryTreeStorage binaryTreeStorage3 = binaryTreeStorage2;
  
      // output values in data structure to a file
      binaryTreeStorage3.write(out9);
  
      fin6.close();
      out9.close();
  
      // pause
      cout &lt;&lt; endl &lt;&lt; "Finished" &lt;&lt; endl;
      int keypress; cin &gt;&gt; keypress;
      return 0;
  }
  </code></pre>
  
  <p>// Copy Constructor</p>
  
  <pre><code>BinaryTreeStorage::BinaryTreeStorage(BinaryTreeStorage &amp;source)
  {
      if(source.root == NULL)
          root = NULL;
      else
          copyTree(this-&gt;root, source.root);
  }
  </code></pre>
  
  <p>// Copy Tree Method</p>
  
  <pre><code>void BinaryTreeStorage::copyTree(node *thisRoot, node *sourceRoot)
  {
      if(sourceRoot == NULL)
      {
          thisRoot = NULL;
      }
      else
      {
          thisRoot = new node;
          thisRoot-&gt;nodeValue = sourceRoot-&gt;nodeValue;
          copyTree(thisRoot-&gt;left, sourceRoot-&gt;left);
          copyTree(thisRoot-&gt;right, sourceRoot-&gt;right);
      }
  }
  </code></pre>