I'm having some problems defining some operator overloads for template classes. Let's take this hypothetical class for example.
template <class T>
class MyClass {
// ...
};
operator+=
// In MyClass.h MyClass<T>& operator+=(const MyClass<T>& classObj); // In MyClass.cpp template <class T> MyClass<T>& MyClass<T>::operator+=(const MyClass<T>& classObj) { // ... return *this; }
Results in this compiler error:
no match for 'operator+=' in 'classObj2 += classObj1'
operator<<
// In MyClass.h friend std::ostream& operator<<(std::ostream& out, const MyClass<T>& classObj); // In MyClass.cpp template <class T> std::ostream& operator<<(std::ostream& out, const MyClass<T>& classObj) { // ... return out; }
Results in this compiler warning:
friend declaration 'std::ostream& operator<<(std::ostream&, const MyClass<T>&)' declares a non-template function
What am I doing wrong here?
See Question&Answers more detail:os