fork download
  1. /// \brief The free-function template,
  2. /// which is overloading a method with the same name in AbstractA below.
  3. template <class T>
  4. inline const T overloadedMethod(const T& lhs, const T& rhs)
  5. {
  6. return T(lhs.value+rhs.value);
  7. }
  8.  
  9. /// \brief AbstractA class
  10. class AbstractA
  11. {
  12. public:
  13. AbstractA (int aVal):
  14. value(aVal)
  15. {}
  16.  
  17.  
  18. inline const AbstractA overloadedMethod(const AbstractA &rhs) const
  19. {
  20. return AbstractA(value+rhs.value);
  21. }
  22.  
  23. protected:
  24. int value;
  25. };
  26.  
  27. /// \brief A class, deriving from AbstractA,
  28. /// and friending the free-function template.
  29. class A : public AbstractA
  30. {
  31. friend const A overloadedMethod <A>(const A& lhs, const A& rhs);
  32. /// This one gives me compilation error
  33. //template<class T> friend const T overloadedMethod(const T& lhs, const T& rhs);
  34. /// This one would be okay
  35.  
  36. public:
  37. A (int aVal):
  38. AbstractA(aVal)
  39. {}
  40. };
  41.  
  42. int main()
  43. {
  44. A a1(1), a2(2);
  45. overloadedMethod(a1, a2);
  46.  
  47. return 0;
  48. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:31:20: error: field ‘overloadedMethod’ has incomplete type
     friend const A overloadedMethod <A>(const A& lhs, const A& rhs);
                    ^
prog.cpp:31:20: error: expected ‘;’ at end of member declaration
prog.cpp:31:37: error: expected unqualified-id before ‘<’ token
     friend const A overloadedMethod <A>(const A& lhs, const A& rhs);
                                     ^
prog.cpp: In instantiation of ‘const T overloadedMethod(const T&, const T&) [with T = A]’:
prog.cpp:45:27:   required from here
prog.cpp:24:9: error: ‘int AbstractA::value’ is protected
     int value;
         ^
prog.cpp:6:23: error: within this context
     return T(lhs.value+rhs.value);
                       ^
prog.cpp:24:9: error: ‘int AbstractA::value’ is protected
     int value;
         ^
prog.cpp:6:23: error: within this context
     return T(lhs.value+rhs.value);
                       ^
stdout
Standard output is empty