fork(1) download
  1. // How NOT TO DO IT
  2.  
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. template <typename T>
  7. struct Foo
  8. {
  9. friend ostream& operator<<(ostream &output, const Foo& rhs);
  10.  
  11. };
  12.  
  13. template <class T>
  14. ostream& operator<<(ostream &output, const Foo<T>& rhs)
  15. {
  16. return output; // don't do anything
  17. }
  18.  
  19. int main()
  20. {
  21. Foo<int> foo;
  22. std::cout << foo << std::endl;
  23. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:9:63: warning: friend declaration 'std::ostream& operator<<(std::ostream&, const Foo<T>&)' declares a non-template function [-Wnon-template-friend]
     friend ostream& operator<<(ostream &output, const Foo& rhs);
                                                               ^
prog.cpp:9:63: note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) 
/home/3GzhQ0/ccH29znL.o: In function `main':
prog.cpp:(.text.startup+0x1b): undefined reference to `operator<<(std::ostream&, Foo<int> const&)'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty