3

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.

2 Answers2

3

As @user888379 pointed out, moving the implementation of showA and showB methods after both classes have been completely declared will solve your problem. Following is working code:

#include <iostream> 

class A;

class B
{
private:
    int bVariable;
public:
    B() :bVariable(9){}
    void showA(A &myFriendA);
    friend class A;  // Friend Class 
};

class A
{
private:
    int aVariable;
public:
    A() :aVariable(7){}
    void showB(B &myFriendB);
    friend class B;  // Friend Class 
};

void B::showA(A &myFriendA) {
    std::cout << "A.aVariable: " << myFriendA.aVariable << std::endl; // Since B is friend of A, it can access private members of A 
}

void A::showB(B &myFriendB) {
    std::cout << "B.bVariable: " << myFriendB.bVariable << std::endl; // Since A is friend of B, it can access private members of B
}

int main() {
    A a;
    B b;
    b.showA(a);
    a.showB(b);
    return 0;
}

Read this answer for more detailed analysis.

Shubham Agrawal
  • 182
  • 2
  • 12
3

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;
}
Ian
  • 172
  • 9