title: Overloading operator == in a class that uses another class
body: <p>I'm beginning to learn C++ and object-oriented programming and I'm struggling to understand what is wrong with my assignment.
  I have two classes: point and line to define points and lines in a Cartesian plan. I'm trying to overload == operator for line class.</p>
  
  <p>Definition of class in point.h:</p>
  
  <pre><code>#ifndef POINT_H
  #define POINT_H
  
  #include &lt;iostream&gt;
  
  class point
  {
      public:
  
          // Default constructor
          // Initializes (x,y) = (0,0)
          point();
  
          // Overloaded constructor
          point(double , double);
  
          // Copy constructor
          point(const point&amp; );
  
          // Sets new value for x
          void set_x (double );
  
          // Sets new value for y
          void set_y(double );
  
          // Returns value of x
          double get_x();
  
          // Returns value of y
          double get_y();
  
          // Overloads = operator
          void operator = (const point&amp; );
  
      private:
          double x, y;
  
  };
  
  // Overloads == operator
  bool operator ==(point&amp; , point&amp; );
  
  #endif
  </code></pre>
  
  <p>Implementation in point.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "point.h"
  
  using namespace std;
  
  // Default constructor
  // Initializes (x,y) = (0,0)
  point::point() {
      x = 0;
      y = 0;
  }
  
  // Overloaded constructor
  point::point(double X, double Y) {
      x = X;
      y = Y;
  }
  
  // Copy constructor
  point::point(const point&amp; p) {
      this-&gt;x = p.x;
      this-&gt;y = p.y;
  }
  
  // Sets new value for x
  void point::set_x (double X) {
      x = X;
  }
  
  // Sets new value for y
  void point::set_y(double Y) {
      y = Y;
  }
  
  // Returns value of x
  double point::get_x() {
      return x;
  }
  
  // Returns value of y
  double point::get_y() {
      return y;
  }
  
  // Overloads = operator
  void point::operator = (const point&amp; source) {
      this-&gt;x = source.x;
      this-&gt;y = source.y;
  }
  
  // Overloads == operator
  bool operator == (point&amp; p1, point&amp; p2) {
      if(p1.get_x() == p2.get_x() &amp;&amp; p1.get_y() == p2.get_y()) {
          return true;
      }
      return false;
  }
  </code></pre>
  
  <p>The line.h file:</p>
  
  <pre><code>#ifndef LINE_H
  #define LINE_H
  
  #include &lt;iostream&gt;
  #include "point.h"
  
  class line {
      public:
  
          // line = X axis
          line();
  
          // line passes through (0,0) and
          // the point provided by programmer
          line(point );
  
          // line passes through the 2
          // points provided
          line(point , point );
  
          // Copy Constructor
          line(line&amp; );
  
          // Overloads = operator
          void operator = (line&amp; );
  
          // Overloads == operator
          friend bool operator ==(const line&amp; , const line&amp; );
  
  
      private:
          point p1;
          point p2;
  };
  
  #endif
  </code></pre>
  
  <p>The line.cpp:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "point.h"
  #include "line.h"
  
  using namespace std;
  
  // line = X axis
  line::line() {
      p1.set_x(0);
      p1.set_y(0);
      p2.set_x(100);
      p2.set_y(0);
  }
  
  // line passes through (0,0) and point p
  line::line(point p) {
      p1.set_x(0);
      p1.set_y(0);
      p2.set_x(p.get_x());
      p2.set_y(p.get_y());
  }
  
  // line passes through the 2 points p &amp; q
  line::line(point p, point q) {
      p1.set_x(p.get_x());
      p1.set_y(p.get_y());
      p2.set_x(q.get_x());
      p2.set_y(q.get_y());
  }
  
  // Copy Constructor
  line::line(line&amp; L) {
      this-&gt;p1 = L.p1;
      this-&gt;p2 = L.p2;
  }
  
  // Overloads = operator
  void line::operator = (line&amp; L) {
      this-&gt;p1.set_x(L.p1.get_x());
      this-&gt;p1.set_y(L.p1.get_y());
      this-&gt;p2.set_x(L.p2.get_x());
      this-&gt;p2.set_y(L.p2.get_y());
  }
  
  bool operator ==(const line&amp; L1, const line&amp; L2) {
      if((L1.p1 == L2.p1) &amp;&amp; (L1.p2 == L2.p2)) {
          return true;
      }
      return false;
  }
  </code></pre>
  
  <p>The == operator for line doesn't work... The code compiles without any error or warning but when I try to use it... it will give me a message: <strong>Segmentation fault: 11</strong>. </p>
  
  <p>I'm calling these methods in this way:</p>
  
  <pre><code>int main (int argc, char const* argv[])
  {
      point p1(10, 10), p2(13, 18.5), p3(0,0);
      line l1, l2(p1,p3), l3(p1,p2);
      l1 = l2;
  
      if(l1 == l2) {
          cout &lt;&lt; "l1 == le" &lt;&lt; endl;
      }
      else {
          cout &lt;&lt; "l1 != l2" &lt;&lt; endl;
      }
  
      return 0;
  }
  </code></pre>