0

The private data member is inaccessible. Although i have declared function as friend of class. Can anyone help me.

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

Error Picture is attached.

Private Data member inaccessible

Chad
  • 18,706
  • 4
  • 46
  • 63
Shoaib Khan
  • 51
  • 1
  • 10
  • 2
    works in gcc 6.3: https://ideone.com/uFov6b Also, works in VS2017 – Chad Mar 28 '18 at 22:47
  • The code compiles [just fine](https://wandbox.org/permlink/QGmek3InTQGDa90b), and that's not a compiler error. – super Mar 28 '18 at 22:48
  • 1
    Can't reproduce in MSVS2015 or 2010 – user4581301 Mar 28 '18 at 22:51
  • 1
    Have you actually built it? Those looks like Intellisense _errors_ which is where the compiler is just guessing at things, but if you build it it should build fine. – Tas Mar 28 '18 at 22:51
  • If it's really an intellisense error: Intellisense (AKA Dumbisense) often is wrong (although it has become a lot better the last years). The occasional wrong wiggly lines in the code drawn by Intellisense are easy to ignore, but when the wrong errors show up in the error list, they become really confusing. That's why I never have Intellisense errors shown in the Error list (you can switch it off with the dropdown menu right to "0 Messages" – user2328447 Mar 28 '18 at 23:25
  • I'm also using MVS10 and MVS15, 2 ERROR is generated there. – Shoaib Khan Mar 29 '18 at 06:25
  • Error is generated only MVS13 and MVS15, i used MVS10 error is not generated. It ran smooth. Why is it so? – Shoaib Khan Mar 29 '18 at 07:07

0 Answers0