Operator-Überladung bei Klassenvorlagen

Ich habe einige Probleme beim Definieren von Operatorüberladungen für Vorlagenklassen. Nehmen wir zum Beispiel diese hypothetische Klasse.

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;
}

Ergebnisse in diesem Compilerfehler:

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;
}

Ergebnisse in dieser Compiler-Warnung:

friend declaration 'std::ostream& operator<<(std::ostream&, const MyClass<T>&)' declares a non-template function

Was mache ich hier falsch?

Antworten auf die Frage(10)

Ihre Antwort auf die Frage