0

Is it possible to create a class that has a friend function of a forward declared class? If I wanted to create two classes that have friend functions of each other, how would I do that? This attempt fails with the message invalid use of incomplete type 'class Y':

class Y;

class X
{
public:
    void a();
    friend void Y::b();
};

class Y
{
public:
    void b();
    friend void X::a();
};

I also tried forward declaring void Y::b(); right after class Y; which similarly did not work. Would this example be an indicator of bad design on my part or just a limitation of C++?

FuzzyCat444
  • 315
  • 2
  • 7
  • 1
    It's hard to say whether something is a good design without knowing what's it's intended to accomplish--but it does seem like a design that would require pretty careful justification, anyway. – Jerry Coffin Nov 05 '20 at 01:48
  • Make either class Y a friend of class X or vice versa. As far as I know C++ does not allows that. By the way, it somewhat suspicious that you need bidirectionnal friendship. You might want to reconsider your design. – Phil1970 Nov 05 '20 at 02:10

0 Answers0