我有两个班级:
class CALLDB;
class CALL
{
friend class CALLDB;
public:
string GetStart()const;
private:
string start;
};
第二节课:
class CALLDB
{
friend class CALL;
public:
unsigned int Load(istream& fin);
private:
unsigned int numCalls;
};
在主要功能中,我这样做了:
int main(){
CALLDB calldata;
cout<<calldata.numCalls;
}
然后它说:
错误C2248:'CALLDB :: numCalls':无法访问类'CALLDB'中声明的私有成员
为什么会这样?我的朋友班宣言有问题吗?
答案 0 :(得分:0)
#include <iostream>
Using namespace std;
class CALLDB;
class CALL
{
friend class CALLDB;
private:
void display()
{
cout<<"\n from Private function of display() of the class CALL ";
}
};
class CALLDB
{
friend class CALL;
public:
void output()
{
CALL ca;
cout<<"\n from public function output of CALLDB class ";
cout<<"\n Calling of private function display of class CALL";
ca.display();
}
};
int main()
{
CALLDB cd1;
cd1.output();
}
输出
来自CALLDB类的公共函数输出
调用CALL类的私人功能显示
来自CALL类的display()的私有函数