0

I want to overload the << operator to accept an object of the nested class and print the items of the nested class. This is very easy if I define the friend function inside the nested class like

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

But when I define it outside

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

I get the following error

error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream}' and 'X::Y')

Here is the main function

main(){
    X<int> x;
    cout << x.test;
}
CodeMan
  • 45
  • 6

0 Answers0