私人数据成员无法访问。虽然我已宣布职能为班级的朋友。 谁能帮我。
class ONE;
class TWO {
public:
void print(ONE& x);
};
class ONE {
private:
int a, b;
public:
friend void TWO::print(ONE& x);
ONE() : a(1), b(2) { }
};
void TWO::print(ONE& x) {
cout << "a is " << x.a << endl;
cout << "b is " << x.b << endl;
}
int main() {
ONE xobj;
TWO yobj;
yobj.print(xobj);
}
错误图片已附加。