Hello people,
I didn't write any C++ code and became little rusty.
I cannot figure why the following code:
generate linker error?Code:#include <iostream> using namespace std; template <typename T> class Int { T x; public: Int(int); Int(const Int<T>&); Int<T>& operator =(const Int<T>&); friend Int<T> operator +(const Int<T>&, const Int<T>&); void show() { cout << x; } }; template <typename T> Int<T>::Int(int t):x(t) { } template <typename T> Int<T>::Int(const Int<T>& ti) { x = ti.x; } template <typename T> Int<T>& Int<T>::operator =(const Int<T>& ti) { if (this != &ti) { x = ti.x; } return *this; } template <typename T> Int<T> operator +(const Int<T>& first, const Int<T>& second) { return Int<T>(first.x+second.x); } int main(int argc, char *argv[]) { Int<int> a(4), b(5), c(0); c = a+b; c.show(); return 0; }
Can friend function be used when working with templates?
If I overload operator + as member function, everything works.
What would be explanation for such behavior?
Thanks



LinkBack URL
About LinkBacks



) as such, their template parameter(s) is the one you specified for the class as a whole. 