rvedtsaneon
rvedtsaneon

Reputation: 13

Member access into incomplete type

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.

Upvotes: 0

Views: 3059

Answers (1)

HolyBlackCat
HolyBlackCat

Reputation: 96042

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.

Upvotes: 3

Related Questions