C ++ namespaces, inner classes and operator resolution
In the C ++ namespace myspace
, I have a class Outer
that in turn has an inner class Inner
. While I can declare and define a global friends operator QDataStream& operator<<(QDataStream& s, const myspace::Outer& o)
, I cannot see how to declare a global friends operator QDataStream& operator<<(QDataStream& s, const myspace::Outer::Inner& o)
. The lines written represent a failed attempt. I can't see how to declare an inner class without defining the outer one.
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've read Namespaces and Operator Resolution , C ++ Defining <Inner Class Operator , Accessing a Private Class in a <<Operator in a Namespace, and Operator Overloading, Name and Namespace Resolution , but none of them work.
If I uncomment these lines, the former throws the error "external.h: 7: error:" Inner "in" class myspace :: Outer "does not name the class type Outer :: Inner; ^" It seems to be a key. I cannot declare an inner class.
I am using C ++ 11.
This question is not a duplicate of the Forward nested types / classes declaration in C ++ if it can be resolved without direct reference.
Given the time span, 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;
}
}