title: Vector returns negative size c++
body: <p>For an exercize, I want to print out a tree data structure that is based on Node objects. This means, every object has a vector nodes that again holds other objects of type Node. But for some reason, when I let print out this->get_nr_children of the leaf nodes which basically just returns nodes.size(), I get completely random (negative) Integers where it should actually return 0. The even more interesting part: Every time I compile and execute, it prints out different Integers that alway are some low negative numbers. I do not have a clue what is happening!</p>
  
  <p>Node.h</p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;vector&gt;
  
  using namespace std;
  
  class Node
  {
  public:
      virtual ~Node();
      Node(string name = "");
      string get_name() const;
      void set_name(string&amp; new_name);
      int get_nr_children() const;
      Node* get_child(int i) const;
      void add_child(Node child);
      void create_complete_tree(int nr_child_nodes, int tree_depth);
      void print();
  
  private:
      string name;
      static int node_id;
      vector&lt;Node&gt; nodes = {};
  };
  </code></pre>
  
  <p>Node.cpp</p>
  
  <pre><code>#include "node.h"
  #include &lt;sstream&gt;
  
  using namespace std;
  
  Node::Node(string name) {
      node_id++;
      nodes = {};
      if (name == "") {
          stringstream str_sm;
          str_sm &lt;&lt; (node_id);
          string node_id_str = str_sm.str();
          this-&gt;name = "node_" + node_id_str;
      } else {
          this-&gt;name = name;
      }
  }
  
  Node::~Node() {
      nodes.clear();
      // node_id = 0;
  }
  
  int Node::node_id = 0;
  
  string Node::get_name() const {
      return name;
  }
  
  void Node::set_name(string&amp; new_name) {
      this-&gt;name = new_name;
  }
  
  
  int Node::get_nr_children() const {
      return nodes.size();
  }
  
  Node* Node::get_child(int i) const {
      if (i &gt;= nodes.size()) {
          return NULL;
      }
      Node node = nodes[i];
      Node *ptrNode = &amp;node;
      return ptrNode;
  }
  
  void Node::add_child(Node child) {
      nodes.push_back(child);
  }
  
  void Node::create_complete_tree(int nr_child_nodes, int tree_depth) {
      tree_depth--;
      if (tree_depth &lt;= 0) {
          return;
      }
      for (int i = 0; i &lt; nr_child_nodes; i++) {
          Node* node = new Node();
          this-&gt;add_child(*node);
          node-&gt;create_complete_tree(nr_child_nodes, tree_depth);
      }
  }
  
  void Node::print() {
      cout &lt;&lt; this-&gt;get_name() &lt;&lt; "\n";
      cout &lt;&lt; "I got this many children " &lt;&lt; this-&gt;get_nr_children();
      for (int i = 0; i &lt; this-&gt;get_nr_children(); i++) {
          cout &lt;&lt; "\t";
          this-&gt;get_child(i)-&gt;print();
          cout &lt;&lt; "\n";
      }
  }
  </code></pre>
  
  <p>main.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "node.cpp"
  
  using namespace std;
  
      int main() {
          Node* root = new Node("root");
          Node* left_child = new Node("left child");
          Node* right_child = new Node("right child");
  
          root-&gt;add_child(*left_child);
          root-&gt;add_child(*right_child);
  
          root-&gt;print();
  
          return 0;
      }
  </code></pre>
  
  <p>When I execute it I get:</p>
  
  <blockquote>
    <p>root I got this many children 2   left child I got this many children
    -62802357     right child I got this many children -62802357</p>
    
    <p>Process finished with exit code 0</p>
  </blockquote>