title: Overloading operator<< c++; undefined reference to `std::basic_ostream
body: <p>I am trying to overload operator&lt;&lt; in my code. If I comment out the lines where I try to use the &lt;&lt; operator with my custom class, it compiles fine. The error almost looks like it doesn't like the c++ libraries (?).</p>
  
  <p>All my research on this problem has stated that it is a linking problem. Most suggest using g++ instead of gcc. I am using g++ as my compiler, and I get this error still.</p>
  
  <p>Code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  //prototype the class and the functions
  template&lt;class T&gt; class strange;
  template&lt;class T&gt; ostream&amp; operator&lt;&lt; (ostream&amp; osObject, strange&lt;T&gt;&amp; sObject);
  
  
  //begin class
  template &lt;class T&gt;
  class strange
  {
      public:
          // .... function prototypes go here.
              strange(T x,T y);
              friend ostream&amp; operator&lt;&lt; &lt;&gt; (ostream&amp; osObject, strange&lt;T&gt;&amp; sObject);
  
      private:
      T a;
      T b;
  };
  // .... your function definitions go here
  template &lt;class T&gt;
          strange&lt;T&gt;::strange(T first, T second){
          a = first;
          b = second;
  }
  
  template &lt;class T&gt;
  ostream&amp; operator&lt;&lt; (ostream&amp; osObject, const strange&lt;T&gt;&amp; sObject){
          osObject &lt;&lt; sObject.a &lt;&lt; ", " &lt;&lt; sObject.b;
          return osObject;
  }
  
  
  
  int main()
  {
      strange&lt;int&gt; x1(4,6) , x2(12,2) ;
      //strange&lt;char&gt; y1('m','n') , y2('m','n') ;
      cout &lt;&lt; "x1 = " &lt;&lt; x1 &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>test.cpp:(.text+0x7a): undefined reference to `std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; operator&lt;&lt; &lt;int&gt;(std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;, strange&lt;int&gt;&amp;)'
  collect2: ld returned 1 exit status
  </code></pre>
  
  <p>Any idea what is causing this?</p>