#include <iostream>
using namespace std;
class CBase
{
public:
int a;
int b;
private:
int c;
int d;
protected:
int e;
int f;
//friend int cout1();
};
class CDerived : public CBase
{
public:
friend class CBase;
int cout1()
{
cout << "a" << endl;
cin >> a;
cout << "b" << endl;
cin >> b;
cout << "c" << endl;
cin >> c;
cout << "d" << endl;
cin >> d;
cout << "e" << endl;
cin >> e;
cout << "f" << endl;
cin >> f;
cout << a << "" << b << "" << c << "" << d << "" << e << "" << f << "" << endl;
}
};
int main()
{
CDerived chi;
chi.cout1();
}
如何使用朋友类和朋友功能?请帮助我。我有很多类似的错误:如何使用朋友功能或朋友类?
c6.cpp: In member function int CDerived::cout1():
c6.cpp:10: error: int CBase::c is private
c6.cpp:30: error: within this context
c6.cpp:11: error: int CBase::d is private
c6.cpp:32: error: within this context
c6.cpp:10: error: int CBase::c is private
c6.cpp:37: error: within this context
c6.cpp:11: error: int CBase::d is private
c6.cpp:37: error: within this context
你需要使cout1返回一些东西来编译 – doctorlove
谢谢,为什么你不应该复制粘贴的另一个例子。 (这是关于我吸取教训的时间) – RobbieE
非常感谢。 。你可以请朋友成员fuc帮忙。 。 – brownKnight