[英]C++ namespaces, inner classes and operator resolution
In the C++ namespace myspace
I have a class Outer
which in turn has an inner class Inner
. 在C ++命名空间myspace
我有一个类Outer
,而后者又有一个内部类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)
. 虽然我可以声明和定义全局友元运算符QDataStream& operator<<(QDataStream& s, const myspace::Outer& o)
,但我看不到如何声明全局友元运算符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. 我已经阅读了命名空间和运算符解析 , C ++定义内部类的<<运算符, 在运算符<<中访问私有类的名称空间和运算符重载,名称解析和名称空间 ,但似乎没有工作。
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. 如果我取消注释这些行,第一行给出错误消息“outer.h:7:错误:'内部'在'类myspace :: Outer'中没有命名类型类Outer :: Inner; ^”这似乎是键。 I cannot declare the inner class. 我无法宣布内部阶级。
I am using C++ 11. 我正在使用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. 如果可以在没有前向引用的情况下解决,那么这个问题不是C ++中嵌套类型/类的前向声明的重复。
Given the time lapse, I am posting the correct answer given by Andreas H. 鉴于时间流逝,我发布了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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.