我想重载<< operator接受嵌套类的对象并打印嵌套类的项。如果我在嵌套类中定义友元函数(如
),这非常容易template <typename T>
class X{
public:
class Y{
public:
int y;
friend ostream &operator<<(ostream &o,const Y &yy){
o<<yy.y;
return o;
}
}test;
public:
X(){
test.y=10;
}
};
但是当我在
之外定义它时template <typename T>
class X;
template <typename T>
ostream &operator<<(ostream &,const typename X<T>::Y &);
template <typename T>
class X{
public:
class Y{
public:
int y;
friend ostream &operator<< <T>(ostream &,const Y &);
}test;
public:
X(){
test.y=10;
}
};
template <typename T>
ostream &operator<<(ostream &o, const typename X<T>::Y &yy){
o<<yy.y;
return o;
}
我收到以下错误
错误:&#39;运营商&lt;&lt;&lt;&# (操作数类型是&#39; std :: ostream {aka std :: basic_ostream}&#39;&#39; X :: Y&#39;)
这是主要功能
main(){
X<int> x;
cout << x.test;
}