fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template<class T>
  5. class MyVar
  6. {
  7. int x;
  8.  
  9. public:
  10. friend void printVar(const MyVar & var);
  11. friend void scanVar(MyVar & var);
  12. };
  13.  
  14. template<class T>
  15. void printVar(const MyVar<T> & var) {
  16. std::cout << var.x << std::endl;
  17. }
  18. template<class T>
  19. void scanVar(MyVar<T> & var) {
  20. std::cin >> var.x;
  21. }
  22.  
  23. struct Foo {};
  24. int main() {
  25. // your code goes here
  26. MyVar<Foo> a;
  27. scanVar(a);
  28. printVar(a);
  29. return 0;
  30. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
123
compilation info
prog.cpp:10:40: warning: friend declaration 'void printVar(const MyVar<T>&)' declares a non-template function [-Wnon-template-friend]
  friend void printVar(const MyVar & var);
                                        ^
prog.cpp:10:40: note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) 
prog.cpp:11:33: warning: friend declaration 'void scanVar(MyVar<T>&)' declares a non-template function [-Wnon-template-friend]
  friend void scanVar(MyVar & var);
                                 ^
/home/POhCBl/ccjI3ner.o: In function `main':
prog.cpp:(.text.startup+0x17): undefined reference to `scanVar(MyVar<Foo>&)'
prog.cpp:(.text.startup+0x1f): undefined reference to `printVar(MyVar<Foo> const&)'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty