This sounds like a very obvious question but I can't find anything on the internet. I have a class other that handles a message in one of its methods. To do so, it has to access a private data member of the message, hence it should be friend of it. But I can't get it to compile. No matter the declaration order, it always complains about types being incomplete. How else would I accomplish such a thing?
#include <cstdio>
struct other;
class message
{
friend auto other::handle_message(message) -> void;
int private_ = 10;
};
struct other
{
auto handle_message(message msg) -> void {
printf("Private part of msg is %d!\n", msg.private_);
}
};
int main() {}
What you need is the following
That is the class
othermust be defined before the classmessageand its member functionhandle_messagemust be defined after the definition of the classmessage.Here is a demonstration program.
The program output is