C++ Binary Tree Programming new Nodes leaving Scope issue
<p>I am a java programmer teaching myself C++. </p>

<p>While writing a binary tree I found that my program did not "add" values to the tree. </p>

<pre><code>#include "stdafx.h"
#include &lt;cstdlib&gt;
#include &lt;iostream&gt;
using namespace std;
class BinaryTree {

    struct Node {
    public:
        int val;
        Node* left;
        Node* right;
        Node::Node(int v) {
            val = v;
            left = nullptr;
            right = nullptr;
        }
    };
public:
    BinaryTree() {
        root = nullptr;
    }

    int size = 0;
    int length();
    bool BinaryTree::add(int v);
    void printTree();
private:
    void printTree(Node* n); 
    Node* root;

};
bool BinaryTree::add(int v) {

    if (root == nullptr) {
        root = new Node(v);
        ++size;
        return true;
    }
    Node* ref = root;
    cout &lt;&lt; ref-&gt;val;
    while (ref != nullptr) {
        if (v &lt; ref-&gt;val) {
            ref = ref-&gt;left;
        }
        else if (v &gt; ref-&gt;val) {
            ref = ref-&gt;right;
        }
        else if (v == ref-&gt;val) {
            return false;
        }
    }
    Node *newNode = new Node(v);
    ref = newNode;
    ++size;
    return true;
}
void BinaryTree::printTree() {
    printTree(root);
}
void BinaryTree::printTree(Node* n) {
    if (n == nullptr) {
        return;
    }
    printTree(n-&gt;left);
    cout &lt;&lt; n-&gt;val &lt;&lt; endl;
    printTree(n-&gt;right);
}
int BinaryTree::length() {
    return size;
}
void main(int i) {
    BinaryTree tree = BinaryTree();
    tree.add(6);
    tree.add(3);
    tree.add(5);
    tree.add(7);
    tree.add(1);
    tree.add(0);
    tree.add(0);

    tree.printTree();
    cout &lt;&lt; "binary tree sz is " &lt;&lt; tree.length() &lt;&lt; endl;
    while (true) {};
}
</code></pre>

<p>I have been unable to find the problem in regards to why the tree doesn't commit new Nodes except the root.</p>

<p>I used "new" in the code when writing (ref = new Node) etc in the adds method because this should prevent the new Node from being destroyed once it leaves the scope. </p>

<p>If anyone can enlighten me on this issue I will be greatly thankful. </p>