C++ namespaces, inner classes and operator resolution


Keywords:c++ 


Question: 

In the C++ namespace myspace I have a class Outer which in turn has an inner class Inner. While I can declare and define a global friend operator QDataStream& operator<<(QDataStream& s, const myspace::Outer& o), I cannot see how to declare a global friend operator QDataStream& operator<<(QDataStream& s, const myspace::Outer::Inner& o). The commented out lines represent a failed attempt. I do not see how to declare the inner class without defining the outer.

namespace myspace {
    class Outer;
    //class Outer::Inner;
}

QDataStream& operator<<(QDataStream& s, const myspace::Outer& o);
//QDataStream& operator<<(QDataStream& s, const myspace::Outer::Inner& o);


namespace myspace {

    class Outer {

        friend QDataStream& (::operator <<)(QDataStream&, const Outer&);

        class Inner {
            //friend QDataStream& (::operator <<)(QDataStream&, const Inner&);
            int i;
        };

        int o;
    };

}

I have read Namespaces and operator resolution, C++ Defining the << operator of an inner class, Accessing private class in operator<< in namespace and Operator overloading, name resolution and namespaces, but none seem to work.

If I uncomment these lines, the first gives the error message "outer.h:7: error: 'Inner' in 'class myspace::Outer' does not name a type class Outer::Inner; ^" This seems to be the key. I cannot declare the inner class.

I am using C++ 11.

This question is not a duplicate of Forward declaration of nested types/classes in C++ if it can can be solved without forward reference.


1 Answer: 

Given the time lapse, I am posting the correct answer given by Andreas H.

namespace myspace {
class Outer {
    class Inner {
        friend QDataStream& operator<<(QDataStream&, const Inner&);
        int i;
    };
    friend QDataStream& operator<<(QDataStream&, const Outer&);
    friend QDataStream& operator<<(QDataStream&, const Inner&);
    int o;
};
QDataStream& operator<<(QDataStream& s, const myspace::Outer& o) {
    s << o.o;
    return s;
}
QDataStream& operator<<(QDataStream& s, const myspace::Outer::Inner& i) {
    s << i.i;
    return s;
}
}