64,641
社区成员
发帖
与我相关
我的任务
分享
D:\main.cpp(12) : error C2027: use of undefined type 'AA'
D:\main.cpp(4) : see declaration of 'AA'
D:\main.cpp(27) : error C2248: 'b' : cannot access private member declared in class 'BB'
D:\main.cpp(8) : see declaration of 'b'
#include <iostream>
using namespace std;
class AA;
class BB {
private:
int b;
public:
BB() {b=0;}
void callA(AA aa);
friend void AA::callB(BB bb);
// friend class AA; //如果把上面一行换成这一行,则没有错误。
// 但是,为了限制友元函数范围,不能把范围从AA::callB(BB bb)扩大到AA!!!
};
class AA {
private:
int a;
public:
AA() {a=0;}
void callB(BB bb);
friend void BB::callA(AA aa);
};
void AA::callB(BB bb)
{
cout << "call B " << bb.b << endl;
return;
}
void BB::callA(AA aa)
{
cout << "call A " << aa.a << endl;
}
int main()
{
AA ai;
BB bi;
ai.callB(bi);
bi.callA(ai);
return 0;
}
#include <iostream>
using namespace std;
class AA;
class BB
{
private:
int b;
public:
BB() {b=0;}
friend void callA(AA aa);
friend void callB(BB bb);
};
class AA
{
private:
int a;
public:
AA() {a=0;}
friend void callA(AA aa);
friend void callB(BB bb);
};
void callA(AA aa)
{
cout << "call A " << aa.a << endl;
}
void callB(BB bb)
{
cout << "call B " << bb.b << endl;
return;
}
int main()
{
AA ai;
BB bi;
callB(bi);
callA(ai);
return 0;
}