【发布时间】:2014-09-05 01:21:35
【问题描述】:
我想知道是否有办法让派生类中的方法成为其基类的朋友。比如:
class Derived;
class Base
{
int i, j;
friend void Derived::f();
protected:
Base();
};
class Derived : public Base
{
public:
void f();
};
我得到的错误是:
error: C2027: use of undefined type 'Derived'
see declaration of 'Derived'
error: C2248: 'Base::i' : cannot access private member declared in class 'Base'
see declaration of 'Base::i'
see declaration of 'Base'
error: C2248: 'Base::j' : cannot access private member declared in class 'Base'
see declaration of 'Base::j'
see declaration of 'Base'
error: C2027: use of undefined type 'Derived'
see declaration of 'Derived'
我整天都在为此苦苦挣扎。我发现的所有关于友谊的东西都只使用了分离的类,而不是继承。
【问题讨论】:
-
当你尝试这个时,你从编译器得到什么错误?您采取了哪些措施来解决这些错误?
-
我正在使用 Visual C++ 12 编译器
标签: c++ inheritance encapsulation friend access-control