Well, twoPrintsOne needs to be passed the actual object of ClassOne that it wants to print the private variables of. You can't print m_a and m_b of an object ClassTwo if there is no ClassOne to print. So, just re-write it as accepting a parameter of type ClassOne, and just prefix m_a and m_b with whatever name you give it in the pass.
Silly me, I should have saw something like that. I corrected that function now but it seems to be another problem; the compiler says that ClassTwo.h does not recognise ClassOne as a type.
1 2 3 4 5
In file included from classOne.h:10:0,
from classOne.cpp:8:
classTwo.h:21:27: error: ‘ClassOne’ does not name a type
void twoPrintsOne(const ClassOne& c);
^
So the problem is the include of classTwo.h in classOne.h? If so, how do I get around that?
It depends on how you want to go about it. Because they both depend on the other (which, by the way, is a sign of a design flaw; but since this is just a trial-and-error of friends it's no issue), you have a few options:
-Make the entirety of ClassTwo a friend of ClassOne instead of just the function, removing the #include requirement and letting you avoid the circular inclusion.
-Have ClassOne have a friend function that, too, is a friend of ClassTwo. That way, you can call the function that, in turn, would call the right function in ClassTwo. This way, all you need is a forward declaration in the header and the #include in the implementation file.
Of course, both of those options are not particularly the best- really, this is a demonstration that friend functions have very specific uses. Usually, if you can avoid a friend function, do so- inheritance tends to work much better.