title: Running into issues with my QuadTree Implementation
body: <p>I'm working on implementing a Quad Tree in C++ and for the past amount of time, I haven't been able to solve my segmentation fault error. The thing is, I have pointers all over the place and not quite sure where my problem is. I already have implemented my code in different languages and simply wanted to test my C++ skills but this is getting in the way.</p>
  
  <p>Here's the code:</p>
  
  <p><strong>quadtree.h:</strong></p>
  
  <pre><code>#ifndef QUADTREE_QUADTREE_H
  #define QUADTREE_QUADTREE_H
  
  #endif //QUADTREE_QUADTREE_H
  #include &lt;vector&gt;
  using namespace std;
  
  class Point
  {
  public:
      Point(double x, double y);
      double x;
      double y;
  };
  
  class Rectangle
  {
  public:
      Rectangle(double x, double y, double width, double height);
      double x;
      double y;
      double width;
      double height;
      bool contains(Point* p);
  };
  
  class QuadTree
  {
  public:
      QuadTree();
      QuadTree(Rectangle* boundary, int capacity);
      Rectangle* boundary = new Rectangle(200,200,200,200);
      QuadTree* qt = new QuadTree(boundary, 4);
  //    Rectangle* boundary;
      Point* p;
      int capacity;
      vector&lt;Point*&gt; points;
      QuadTree* NE;
      QuadTree* NW;
      QuadTree* SW;
      QuadTree* SE;
      bool divided;
      void subdivide();
      void buildTree();
      void insertToNode(Point* p);
  
  };
  </code></pre>
  
  <p><strong>quadtree.cpp</strong></p>
  
  <pre><code>#include "quadtree.h"
  #include &lt;random&gt;
  
  
  Point::Point(double x, double y)
  {
      this-&gt;x = x;
      this-&gt;y = y;
  }
  
  Rectangle::Rectangle(double x, double y, double width, double height)
  {
      this-&gt;x = x;
      this-&gt;y = y;
      this-&gt;width = width;
      this-&gt;height = height;
  }
  
  bool Rectangle::contains(Point* p)
  {
      return (p-&gt;x &gt; this-&gt;x - this-&gt;width &amp;&amp; p-&gt;x &lt; this-&gt;x + this-&gt;width &amp;&amp; p-&gt;y &gt; this-&gt;y - this-&gt;height &amp;&amp; p-&gt;y &lt; this-&gt;y + this-&gt;height);
  }
  
  QuadTree::QuadTree() {}
  
  QuadTree::QuadTree(Rectangle* boundary, int capacity)
  {
      this-&gt;boundary = boundary;
      this-&gt;capacity = capacity;
      this-&gt;divided = false;
  }
  
  void QuadTree::subdivide()
  {
      Rectangle* nw = new Rectangle(this-&gt;boundary-&gt;x + this-&gt;boundary-&gt;width/2, this-&gt;boundary-&gt;y - this-&gt;boundary-&gt;height/2, this-&gt;boundary-&gt;width/2, this-&gt;boundary-&gt;height/2);
      Rectangle* ne = new Rectangle(this-&gt;boundary-&gt;x - this-&gt;boundary-&gt;width/2, this-&gt;boundary-&gt;y - this-&gt;boundary-&gt;height/2, this-&gt;boundary-&gt;width/2, this-&gt;boundary-&gt;height/2);
      Rectangle* sw = new Rectangle(this-&gt;boundary-&gt;x + this-&gt;boundary-&gt;width/2, this-&gt;boundary-&gt;y + this-&gt;boundary-&gt;height/2, this-&gt;boundary-&gt;width/2, this-&gt;boundary-&gt;height/2);
      Rectangle* se = new Rectangle(this-&gt;boundary-&gt;x - this-&gt;boundary-&gt;width/2, this-&gt;boundary-&gt;y + this-&gt;boundary-&gt;height/2, this-&gt;boundary-&gt;width/2, this-&gt;boundary-&gt;height/2);
      this-&gt;NE = new QuadTree(ne, 1);
      this-&gt;NW = new QuadTree(nw, 1);
      this-&gt;SW = new QuadTree(sw, 1);
      this-&gt;SE = new QuadTree(se, 1);
      this-&gt;divided = true;
  }
  
  void QuadTree::buildTree()
  {
      std::random_device rd; // obtain a random number from hardware
      std::mt19937 eng(rd()); // seed the generator
      std::uniform_int_distribution&lt;&gt; distr(0, 199); // define the range
  
      for(int i = 0; i &lt; 1; i++)
      {
          Point* p = new Point(distr(eng), distr(eng));
          this-&gt;qt-&gt;insertToNode(p);
      }
  }
  
  void QuadTree::insertToNode(Point* p)
  {
      if(!this-&gt;boundary-&gt;contains(p))
      {
          return;
      }
  
      if(this-&gt;points.size() &lt; this-&gt;capacity)
      {
          this-&gt;points.push_back(p);
      }
      else
      {
          if(!this-&gt;divided)
          {
              this-&gt;subdivide();
          }
  
      }
      this-&gt;NE-&gt;insertToNode(p);
      this-&gt;NW-&gt;insertToNode(p);
      this-&gt;SW-&gt;insertToNode(p);
      this-&gt;SE-&gt;insertToNode(p);
  }
  </code></pre>