mardi 7 avril 2020

How to solve a problem about two c++ header file that refers to each other? [duplicate]

c++ header file "a.h" :

#pragma once
#include "b.h"

struct A {
    void f() {
        b.f();
    }
    B b;
    int i;
};

c++ header file "b.h" :

#pragma once
#include"a.h"

struct B {
    void f() {
        int x = a->i; //error occur
    }
    A* a;
};

"test.cpp":

#include "a.h"

int main(){
    A a;
    a.f();
}

Call 'a->i' in B will trigger error.Why?How to solve it.

Aucun commentaire:

Enregistrer un commentaire