我正在尝试编写一个非常简单的代码作为练习。问题是当我将一个类的成员函数设为另一个类的成员函数时,它说无法访问,但是当我将整个类声明为另一个类的朋友时,它工作正常。
#include <iostream>
using namespace std;
class gpa2;
class gpa1 {
private:
int no1;
int no2;
public:
void setnum1(int n1, gpa2&xp) {
cout << " the friend member function is : " << xp.no4;
}
void setnum2(int n2) {
no2 = n2;
cout << "num2 is : " << no2 << endl;
};
};
class gpa2 {
private:
int no3;
int no4;
friend void gpa1::setnum1(int, gpa2&);
public:
void setnum3(int n3) {
no3 = n3;
cout << "num3 is : " << no3 << endl;
}
void getnum4(int n4) {
cout << "num4 is : " << n4 << endl;
}
};
int main() {
gpa1 g1;
gpa2 g2;
g1.setnum1(15, g2);
g1.setnum2(30);
g2.setnum3(45);
g2.getnum4(50);
return 0;
}