【发布时间】:2018-01-02 06:08:10
【问题描述】:
在 C++ 命名空间myspace 中,我有一个类Outer,它又具有一个内部类Inner。虽然我可以声明和定义全局友元运算符QDataStream& operator<<(QDataStream& s, const myspace::Outer& o),但我看不到如何声明全局友元运算符QDataStream& operator<<(QDataStream& s, const myspace::Outer::Inner& o)。注释掉的行表示失败的尝试。我看不到如何在不定义外部的情况下声明内部类。
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;
};
}
我读过 Namespaces and operator resolution, C++ Defining the << operator of an inner class, Accessing private class in operator<< in namespace 和Operator overloading, name resolution and namespaces,但似乎都不起作用。
如果我取消注释这些行,第一行会给出错误消息“outer.h:7: error: 'Inner' in 'class myspace::Outer' does not name a type 类外::内; ^" 这似乎是关键。我不能声明内部类。
我正在使用 C++ 11。
这个问题不是Forward declaration of nested types/classes in C++的重复,如果它可以在没有前向引用的情况下解决。
【问题讨论】:
-
您可以添加尝试编译失败时收到的错误消息吗?
-
我在第一个注释行未注释时添加了错误消息。
-
你是对的,这是重复的。在 C++ 中似乎是不可能的。
-
这不是重复的 - 不需要前向声明 由于重复标记,我无法添加一个简单的解决方案:namespace myspace { class Outer {friend QDataStream& operator
-
@AndreasH 你需要创建内部类
public。原文为private。在这种情况下没有解决方案。但我同意,它不是完全重复的。
标签: c++ namespaces global operator-keyword