0
struct B;
struct A {
    void funcA(B &b) {
        b.funcB(*this);  // Here where is an error: Member access into incomplete type 'B'
    }
};
struct B {
    A a;
    void funcB(A a1) {
    }
};

As I understand, I need to somehow define B::funcB, because when I swap function body, the error occurs in the A struct.

east1000
  • 1,240
  • 1
  • 10
  • 30
  • 2
    What does [your favourite C++ textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) say about splitting declaration and definition (or about header files and implementation files)? – Yksisarvinen Sep 18 '21 at 19:14
  • 1
    I didn't read any programming books – rvedtsaneon Sep 18 '21 at 19:18
  • Well, I suggest getting one. C++ is a difficult language to take up and without a good book you will struggle a lot with things like that. – Yksisarvinen Sep 18 '21 at 19:45

1 Answers1

3

Define the function outside of the class body:

struct B;

struct A
{
    void funcA(B &b);
};

struct B
{
    A a;
    void funcB(A a1) {}
};

void A::funcA(B &b)
{
    b.funcB(*this);
}

Note that because B contains a member variable of type A (as opposed to e.g. A *), it's impossible to define struct B before struct A.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207