我在为模板类定义一些运算符重载遇到了一些问题。让我们以这个假设的类为例。
在这个编译器错误中出现了以下结果:
在这个编译器警告中显示的结果:
我在这里做错了什么?
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;
}
在这个编译器错误中出现了以下结果:
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;
}
在这个编译器警告中显示的结果:
friend declaration 'std::ostream& operator<<(std::ostream&, const MyClass<T>&)' declares a non-template function
我在这里做错了什么?