Access functions between friend classes

I have the following code. I don't understand why i don’t have access to functions and members of class a.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class b;
class a
{
    friend class b;
public:
    a() {};
    void seta()
    {
        i=10;
    }
private:
    int i;
};
class b
{
    friend class a;
    b() {};
    void setb()
    {
        i=23;
        a::i=23;
        seta();
    }
private:
    int ii;
};


Compiling this snippet with g++ i haven’t access to members...
Compiler says ....
i=23;//error: ‘i’ was not declared in this scope
a::i=23;//error: invalid use of non-static data member ‘a::i’
seta();//error: ‘seta’ was not declared in this scope
Last edited on
To access a non-static member of A, an object of type A is required.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class B ;

class A
{
    private: int ia = 0 ;

    friend B ; // B is a friend of A
};

class B
{
    public:

        void set( A& a2 )
        {
            ib = 123 ;
            a.ia = 4567 ; // friend: can access private member of A
            a2.ia += a.ia ; // friend: can access private member of A
        }

    private:
        A a ;
        int ib = 0 ;
};
Thank you very much.
Jim.
Registered users can post here. Sign in or register to post.