vendredi 28 mai 2021

2 way friendship and between classes and it's solution as forward declaration

I wanted to know if there's any exsisting solution to the problem:

class b;
class a {
  int x;

 public:
  friend class b;
  a() { x = 5 }
  void print(b obj) {
    cout << x << endl;
    cout << obj.y << endl;
  }
};

class b {
  int y;

 public:
  friend class a;
  b() { y = 10 }
  void print(a obj) {
    cout << y << endl;
    cout << obj.x << endl;
  }
};

"This is giving me an issue since class b body is not define before class a, so what is there any easy and existing way to make it work?

Aucun commentaire:

Enregistrer un commentaire