В приведенном ниже коде у меня есть класс шаблона с типом члена (элемент структуры). Я хочу перегрузить оператор‹‹ для этого типа члена. Однако код не будет компилироваться.
Я был бы признателен, если бы кто-нибудь мог указать, где я ошибаюсь?
include <iostream>
using namespace std;
// fwd decl
template<class T> class CC;
// operator<< overload template for member type CC<T>::Element
template<class T>
ostream& operator<<(ostream& os, const typename CC<T>::Element& elm) {
return os << elm.val1 << "/" << elm.val2;
}
template<class T>
class CC
{
public:
struct Element { // type member
int val1;
int val2;
};
template<typename U>
friend ostream& operator<<(ostream& os, const typename CC<U>::Element& elm);
};
int main() {
CC<int>::Element elm{0,0};
cout << elm << endl; // does not compile due to this!
}