title: Why do I need operator overloading?
body: <p>need advice please. The code below, I'm simply trying to cout the x and y values stored in Line2D class. But i get this error.</p>
  
  <pre><code>Assn3.cpp:166:34: error: no match for ‘operator&lt;&lt;’ in ‘std::operator&lt;&lt; [with _Traits = std::char_traits&lt;char&gt;]((* &amp; std::cout), ((const char*)"Pt1: ")) &lt;&lt; l2d.Line2D::getPt1()’
  </code></pre>
  
  <p>Do I need operator overloading? if yes, why? 
  I tried the same for other classes, no error.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>vector&lt;Line2D&gt; l2dvector;
  Line2D l2d;
  
  void readData()
  {
      cout&lt;&lt; "Please enter filename : ";
      cin &gt;&gt; inputFile;
  
      fstream fileStream;
          fileStream.open(inputFile.c_str(),fstream::in);
          int records = 0;
  
      while( fileStream.good() ) 
      {
          string line = "";
          while (getline (fileStream, line))  
          {   
              stringstream ss (line);
              getline (ss, className, ',');
  
                   if (className == "Line2D")
              {   
                  int x1, x2, y1, y2;
                  getline (ss, l2dX1, '[');
                  getline (ss, l2dX1, ',');
  
                  getline (ss, l2dY1, ' ');
                  getline (ss, l2dY1, ']');
  
                  getline (ss, l2dX2, ' ');
                  getline (ss, l2dX2, '[');
                  getline (ss, l2dX2, ',');
  
                  getline (ss, l2dY2, ' ');
                  getline (ss, l2dY2, ']');
  
                  istringstream (l2dX1) &gt;&gt; x1;
                  istringstream (l2dX2) &gt;&gt; x2;
                  istringstream (l2dY1) &gt;&gt; y1;
                  istringstream (l2dY2) &gt;&gt; y2;
  
                  Point2D pt1 (x1,y1);
                  Point2D pt2 (x2,y2);
  
                  Line2D l2d (pt1, pt2);
                  l2dvector.push_back(l2d);
                  l2d.setPt1(pt1);
                  l2d.setPt2(pt2);
  
              //cout &lt;&lt; "class name: " &lt;&lt; className &lt;&lt; endl;          
              cout &lt;&lt; "Pt1: " &lt;&lt; l2d.getPt1() &lt;&lt; endl;
              cout &lt;&lt; "Pt2: " &lt;&lt; l2d.getPt2() &lt;&lt; endl;
              }
                  }
  
      records++;
      }
  
      cout &lt;&lt; records &lt;&lt; "records read in successfully!" &lt;&lt; endl;
      cout &lt;&lt; "Going back to main menu .. " &lt;&lt; endl;
  
  }
  
  
  
  
  Line2D.cpp
  
  Line2D::Line2D ()
  {
  
  }
  
  Line2D::Line2D (Point2D pt1, Point2D pt2)
  {
      this-&gt;pt1 = pt1;
      this-&gt;pt2 = pt2;
  }
  
  
  double Line2D::setLength()
  {
      length = sqrt(pow((pt1.getX()-pt2.getX()),2)) + pow ((pt1.getY()-pt2.getY()),2);
      return length;
  }
  
  
  //Accessors
  
  Point2D Line2D::getPt1()
  {
      return pt1;
  }
  
  
  Point2D Line2D::getPt2()
  {
      return pt2;
  }
  
  double Line2D::getScalarValue() //returns the value of attribute length
  {
      return length;
  }
  
  
  //Mutators
  
  void Line2D::setPt1 (Point2D pt1)
  {
      this-&gt;pt1 = pt1;
  }
  
  void Line2D::setPt2 (Point2D pt2)
  {
      this-&gt;pt2 = pt2;
  }
  </code></pre>
  
  <p>Edit:</p>
  
  <p>Added operator overloading in Point2D.h</p>
  
  <pre><code>ostream &amp;operator&lt;&lt;( ostream &amp;output, const Point2D &amp;P)
                { 
               output &lt;&lt; "X : " &lt;&lt; P.x &lt;&lt; " Y : " &lt;&lt; P.y;
               return output;            
                }
  </code></pre>
  
  <p>Error compiling:</p>
  
  <pre><code>In file included from Point2D.cpp:3:0:
  Point2D.h:39:60: error: ‘std::ostream&amp; Point2D::operator&lt;&lt;(std::ostream&amp;, const Point2D&amp;)’ must take exactly one argument
  In file included from Point3D.h:9:0,
                   from Point3D.cpp:3:
  Point2D.h:39:60: error: ‘std::ostream&amp; Point2D::operator&lt;&lt;(std::ostream&amp;, const Point2D&amp;)’ must take exactly one argument
  In file included from Line2D.h:9:0,
                   from Line2D.cpp:3:
  Point2D.h:39:60: error: ‘std::ostream&amp; Point2D::operator&lt;&lt;(std::ostream&amp;, const Point2D&amp;)’ must take exactly one argument
  </code></pre>