-4

I was reading about friendship in c++(i thought i actually understood it), but when I go to the source code to try it out within some classes, i just don't manage to get it going. I wish to be able to understand WHY it isn't working.

I've already done some research within this website and some others, and i actually found some code that worked, but i really can't see how the logic i'm trying to implement is different from the it:https://www.geeksforgeeks.org/friend-class-function-cpp/

struct B;

struct A{
    A(int _a): a(_a){}
    friend void B::showA(A&);
    private:
    int a;
};

struct B{
    void showA(A&);
};

void B::showA(A& _param){
    cout << _param.a;
}

I expect the function void B::showA(A&) to be able to access the private member "a" of class A, but when i try to compile my code it produces these errors:

friendshipninheritance.cpp(10): error C2027: use of undefined type 'B'

friendshipninheritance.cpp(5): note: see declaration of 'B'

friendshipninheritance.cpp(21): error C2248: 'A::a': cannot access private member declared in class 'A'

friendshipninheritance.cpp(12): note: see declaration of 'A::a'

friendshipninheritance.cpp(7): note: see declaration of 'A'

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
MrPingocho
  • 11
  • 2
  • 1
    "error C2027: use of undefined type 'B'" <-- Should be a big hint. You cannot just forward declare `B`. It needs to be defined *before* `A`. – Jesper Juhl Aug 19 '19 at 17:46
  • 3
    Consider reading a [good C++ book](https://stackoverflow.com/q/388242/) instead of trying to pick up C++ by going to random websites, the latter being a very inefficient and ineffective way of learning a language as sophisticated as C++. – L. F. Aug 19 '19 at 17:48
  • 1
    The title of this question is rather misleading. Your problem is simply that you use something (`B::showA`) before ever declaring _what that thing is_ – Drew Dormann Aug 19 '19 at 17:52

2 Answers2

4

Just reorder declarations.

struct A;

struct B{
    void showA(A&);
};


struct A{
    A(int _a): a(_a){}
    friend void B::showA(A&);
    private:
    int a;
};

void B::showA(A& _param){
    cout << _param.a;
}

Structure A must know the name of the member of the structure B. That is, the definition of B must precede the definition of A so that the name showA is known.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
2

As a rule of a thumb, you should solve compiler errors from top. Usually, one error can spawn more errors, and it's not different in this case.

Your friend declaration was ignored, because compiler doesn't yet know what B is and whether or not it has any function called showA. This lead to all further errors.

You can change the order of declarations to make it work:

struct A;

struct B{
    void showA(A&);
};

struct A{
    A(int _a): a(_a){}
    friend void B::showA(A&);
    private:
    int a;
};

void B::showA(A& _param){
    cout << _param.a;
}
Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52