title: 'friend' functions and << operator overloading: What is the proper way to overload an operator for a class?
body: <p>In a project I'm working on, I have a <code>Score</code> class, defined below in <code>score.h</code>. I am trying to overload it so, when a <code>&lt;&lt;</code> operation is performed on it, <code>_points + " " + _name</code> is printed. </p>
  
  <p>Here's what I tried to do:</p>
  
  <pre><code>ostream &amp; Score::operator&lt;&lt; (ostream &amp; os, Score right)
  {
      os &lt;&lt; right.getPoints() &lt;&lt; " " &lt;&lt; right.scoreGetName();
      return os;
  }
  </code></pre>
  
  <p>Here are the errors returned:</p>
  
  <pre><code>score.h(30) : error C2804: binary 'operator &lt;&lt;' has too many parameters
  </code></pre>
  
  <p>(This error appears 4 times, actually)</p>
  
  <p>I managed to get it working by declaring the overload as a friend function:</p>
  
  <pre><code>friend ostream &amp; operator&lt;&lt; (ostream &amp; os, Score right);
  </code></pre>
  
  <p>And removing the <code>Score::</code> from the function declaration in score.cpp (effectively not declaring it as a member).</p>
  
  <p>Why does this work, yet the former piece of code doesn't?</p>
  
  <p>Thanks for your time!</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I deleted all mentions to the overload on the header file... yet I get the following (and only) error. <code>binary '&lt;&lt;' : no operator found which takes a right-hand operand of type 'Score' (or there is no acceptable conversion)</code> 
  How come my test, in main(), can't find the appropriate overload? (it's not the includes, I checked)</p>
  
  <p>Below is the full score.h</p>
  
  <pre><code>#ifndef SCORE_H_
  #define SCORE_H_
  
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;iostream&gt;
  
  using std::string;
  using std::ostream;
  
  class Score
  {
  
  public:
      Score(string name);
      Score();
      virtual ~Score();
      void addPoints(int n);
      string scoreGetName() const;
      int getPoints() const;
      void scoreSetName(string name);
      bool operator&gt;(const Score right) const;
  
  private:
      string _name;
      int _points;
  
  };
  #endif
  </code></pre>