一段有关友元函数的程序
#include<iostream.h>
class boy
{
int num;
public:
void set_num();
friend int get_total();
};
void boy::set_num()
{
cin>>num;
}
class girl
{
int num;
public:
void set_num()
{cin>>num;}
friend int get_total();
};
int get_total(boy a,girl b)
{
return a.num+b.num;
}
void main()
{
boy a;
girl b;
a.set_num();
b.set_num();
cout<<get_total(a,b);
}
--------------------Configuration: opp3 - Win32 Debug--------------------
Compiling...
opp3.cpp
D:\Program Files\Microsoft Visual Studio\MyProjects\opp3\opp3.cpp(29) : error C2248: 'num' : cannot access private member declared in class 'boy'
D:\Program Files\Microsoft Visual Studio\MyProjects\opp3\opp3.cpp(6) : see declaration of 'num'
D:\Program Files\Microsoft Visual Studio\MyProjects\opp3\opp3.cpp(29) : error C2248: 'num' : cannot access private member declared in class 'girl'
D:\Program Files\Microsoft Visual Studio\MyProjects\opp3\opp3.cpp(20) : see declaration of 'num'
Error executing cl.exe.
opp3.obj - 2 error(s), 0 warning(s)
为什么友元函数不能访问类的私有成员。
用c++ builder也编译不了,请问是哪里错了。谢谢