title: which "<<" operator function should be used?
body: <p>There are two ways of implementing operator "&lt;&lt;" or ">>" function in a project.</p>
  
  <p>1.As a non-member function</p>
  
  <p>2.As a friend </p>
  
  <pre><code>  #include&lt;iostream&gt;
    using namespace std;
    class xxx{
       private: int x;
       public: xxx(int val=0):x(val){}
                int getx(){return x;}
               friend ostream&amp; operator &lt;&lt;(ostream&amp; o, xxx&amp; x1);
    };
    ostream&amp; operator&lt;&lt;(ostream&amp; o, xxx&amp; x1)
    {
      o&lt;&lt;x1.getx();
      return o;
    }
    ostream&amp; operator &lt;&lt;(ostream&amp; o, xxx&amp; x1)
    {
       o&lt;&lt;x1.getx();
       return o;
    }
    int main(int argc, char *argv[])
    {
       xxx x1(5);
       return 0;
    }
  </code></pre>
  
  <p>Looks like both non-member and friend function have same signature when implementing, and thus i get compiler error :
  "error: redefinition of 'std::ostream&amp; operator&lt;&lt;(std::ostream&amp;, xxx&amp;)' ". </p>
  
  <p>Could anyone please help how to compile the above code.</p>
  
  <p>Also would like to know in which situation should we use non-member "operator =" function over friend "operator =" function.</p>