c++friend-class

Two Classes Befriending Each Other


I'm trying to make two classes friend with each other, but I keep getting a "Use of Undefined type A" error message.

Here is my code:

I've tried to add class A; as shown on top but still the same.

#include <iostream> 
class A;
class B
{
private:
    int bVariable;
public:
    B() :bVariable(9){}
    void showA(A &myFriendA)
    {
        std::cout << "A.aVariable: " << myFriendA.aVariable << std::endl;// Since B is friend of A, it can access private members of A 
    }
    friend class A;
};
class A
{
private:
    int aVariable;
public:
    A() :aVariable(7){}
    void showB(B &myFriendB){
        std::cout << "B.bVariable: " << myFriendB.bVariable << std::endl;
    }
    friend class B;  // Friend Class 
};
int main() {
    A a;
    B b;
    b.showA(a);
    a.showB(b);

    system("pause");
    return 0;
}

I'm trying to make class A access class B and vice versa via the friendship.


Solution

  • You can't access myFriendA.aVariable because the compiler doesn't know it exists. All it knows is that a class A exists (because of the forward declaration on the second line), but it hasn't been fully defined so it has no idea what its members/methods are.

    If you wanted to make this work, showA() would have to be declared outside of the class scope.

    class A;
    class B
    {
    private:
        int bVariable;
    public:
        B() :bVariable(9){}
        void showA(A &myFriendA);
    
        friend class A;
    };
    class A
    {
    private:
        int aVariable;
    public:
        A() :aVariable(7){}
        void showB(B &myFriendB){
            std::cout << "B.bVariable: " << myFriendB.bVariable << std::endl;
        }
        friend class B;  // Friend Class 
    };
    
    // Define showA() here
    void B::showA(A &myFriendA)
    {
        std::cout << "A.aVariable: " << myFriendA.aVariable << std::endl;
    }
    
    int main() {
        A a;
        B b;
        b.showA(a);
        a.showB(b);
    
        system("pause");
        return 0;
    }