私人数据成员在朋友功能中无法访问

时间:2018-03-28 22:42:27

标签: c++ friend friend-function

私人数据成员无法访问。虽然我已宣布职能为班级的朋友。 谁能帮我。

    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);
    }

错误图片已附加。

Private Data member inaccessible

0 个答案:

没有答案