title: overloading operator<< for set
body: <p>I have a set of pairs of integers and I want to print it, so I overloaded &lt;&lt; operator for set and pair classes as :</p>
  
  <pre><code>template&lt;typename T, typename U&gt;
  inline ostream&amp; operator&lt;&lt;(ostream&amp; os, pair&lt;T,U&gt; &amp;p){
      os&lt;&lt;"("&lt;&lt;p.first&lt;&lt;","&lt;&lt;p.second&lt;&lt;")";
      return os;
  }
  
  
  template&lt;typename T&gt;
  inline ostream&amp; operator&lt;&lt;(ostream&amp; os, set&lt;T&gt; &amp;s){
      os&lt;&lt;"{";
      for(auto it = s.begin() ; it != s.end() ; it++){
          if(it != s.begin())
              os&lt;&lt;",";
          os&lt;&lt;*it;
      }
      os&lt;&lt;"}";
      return os;
  }
  </code></pre>
  
  <p>When I create a set and output it like</p>
  
  <pre><code>set&lt;pair&lt;int,int&gt;&gt; s;
  cout&lt;&lt;s&lt;&lt;endl;
  </code></pre>
  
  <p>It gives the errors :</p>
  
  <pre><code>cannot bind ‘std::ostream {aka std::basic_ostream&lt;char&gt;}’ lvalue to ‘std::basic_ostream&lt;char&gt;&amp;&amp;’
     os&lt;&lt;*it;
  </code></pre>
  
  <p>and</p>
  
  <pre><code>initializing argument 1 of ‘std::basic_ostream&lt;_CharT, _Traits&gt;&amp; std::operator&lt;&lt;(std::basic_ostream&lt;_CharT, _Traits&gt;&amp;&amp;, const _Tp&amp;) [with _CharT = char; _Traits = std::char_traits&lt;char&gt;; _Tp = std::pair&lt;int, int&gt;]’
       operator&lt;&lt;(basic_ostream&lt;_CharT, _Traits&gt;&amp;&amp; __os, const _Tp&amp; __x)
  </code></pre>
  
  <p>I don't know what is the problem, the errors are very cryptic. Moreover, if I create a set of integers and print it, it works fine.</p>