title: operator overloading c++ / where to put my code?
body: <p>Today I overloaded the &lt;&lt; operator in one of my classes:</p>
  
  <pre><code>#ifndef TERMINALLOG_HH
  #define TERMINALLOG_HH
  
  using namespace std;
  
  class Terminallog {
  public:
  
      Terminallog();
      Terminallog(int);
      virtual ~Terminallog();
  
      template &lt;class T&gt;
      Terminallog &amp;operator&lt;&lt;(const T &amp;v);
  
  private:
  
  };
  
  #endif
  </code></pre>
  
  <p>As you can see I defined the overloaded operator in my header file and I went on implementing it in my .cc file:</p>
  
  <pre><code>//stripped code
  
  template &lt;class T&gt;
  Terminallog &amp;Terminallog::operator&lt;&lt;(const T &amp;v) {
      cout &lt;&lt; endl;
      this-&gt;indent();
      cout &lt;&lt; v;
      return *this;
  }
  
  //stripped code
  </code></pre>
  
  <p>Afterwards I created a main.cpp file using my new class:</p>
  
  <pre><code>#include "inc/terminallog.hh"
  
  int main() {
      Terminallog clog(3);
      clog &lt;&lt; "bla";
      clog &lt;&lt; "bla";
      return 0;
  }
  </code></pre>
  
  <p>and i went on compilying:</p>
  
  <pre><code>g++ src/terminallog.cc inc/terminallog.hh testmain.cpp -o test -Wall -Werror 
  /tmp/cckCmxai.o: In function `main':
  testmain.cpp:(.text+0x1ca): undefined reference to `Terminallog&amp; Terminallog::operator&lt;&lt; &lt;char [4]&gt;(char const (&amp;) [4])'
  testmain.cpp:(.text+0x1de): undefined reference to `Terminallog&amp; Terminallog::operator&lt;&lt; &lt;char [4]&gt;(char const (&amp;) [4])'
  collect2: ld returned 1 exit status
  </code></pre>
  
  <p>BAM! a stupid linker error and I still have no idea where it comes from. I played around a bit and noticed that putting the implementation of my overloaded operator in my header file solves all problems. Now I am even more confused than before.</p>
  
  <p>Why can't I put the implementation of the overloaded operator in my .cc file? Why is it running smoothly when I put it in my header file? </p>
  
  <p>Confused thanks in advance</p>
  
  <p>ftiaronsem</p>