0
votes

In this situation,

A.h

#include "B.h"

class A{
    private:
        static int n;
        friend void B::setN(int _n);
};

A.cpp

#include "A.h"

int A::n = 0;

B.h

class B{
    public:
        static void setN(int _n);
};

B.cpp

#include "B.h"
#include "A.h"

void B::setN(int _n) { A::n = _n; }

IntelliSense: member "A::n" (declared at of "A.h") is inaccessible has occured.

But if I correct friend void B::setN(int _n); to friend class B;, it has no error.

Why can't I use this way?

I hope that only the static member function is accessible.

2
The code seems to work on GCC 4.7. - John Zwinck
I think that if method setN is public and static, by making it friend with A you would allow public access to private members of A. What happens if you make setN private? - VAndrei
I worked with VS2013, But yeah, I tested it on GCC now, and it works. I think I'm missing something else. - Hyunan Kwon
@VAndrei I know it is not different private/public. - Hyunan Kwon

2 Answers

0
votes

missed a semicolon

void B::setN(int _n) { A::n = _n; }
0
votes

It cause IntelliSense Error only, not compile error.