【发布时间】:2014-08-07 02:43:32
【问题描述】:
为什么 main 中的 operator
#include <iostream>
struct Base {};
std::ostream& operator<<(std::ostream& os, const Base&) { return os << "Hi\n"; }
template <class T>
struct Container
{
class Derived : Base {
template <class U>
friend std::ostream& operator<<(std::ostream& os, const typename Container<U>::Derived&);
};
};
template <class U>
std::ostream&
operator<<(std::ostream& os, const typename Container<U>::Derived& der)
{
return os << static_cast<const Base&>(der);
}
int main()
{
Container<int>::Derived d;
std::cout << d;
return 0;
}
g++ 说:
/tmp $ g++ test.c
test.c: In function ‘int main()’:
test.c:28: error: ‘Base’ is an inaccessible base of ‘Container<int>::Derived’
/tmp $
编辑:私有继承是有意的。这就是为什么 operator
EDIT2:好友声明的固定位置;在内部类中。
【问题讨论】:
-
要做
: public Base -
operator<<是Container的朋友,而不是Derived的朋友。 -
哎呀,错字了;从防火墙后面手动复制
标签: c++