I was working through all of the example code in chapter 9, Class Inheritance and Virtual Functions, when I came upon the Class Members as Friends portion of the chapter. It looked really short and I figured I'd knock it out before heading into the section on Virtual Functions.
It didn't turn out that way. Long story short, the example code appeared to be broken, so I consulted the 2010 edition of the book (picked up on a good sale) and the example had been expanded to introduce forward declarations to resolve the issue of one class depending on another class. That combined with moving the implementation of the constructor for the second class (which accepts a reference to the first class) into a .cpp file seemed to have done the trick.
Here's the code: class_mem_as_friends, r23
I had come across Headers and Includes: Why and How while doing a Google search, and I thought I had everything figured out, but when I change the order of the include statements in any of the .cpp files, all sorts of errors are given.
So, I guess my question is this:
Why must CCarton.h be included before CBottle.h ? I feel like I'm overlooking something obvious.
Reading the previously mentioned "Headers and Includes: Why and How" article, I thought that these two items would apply:
I figured that would apply to CBottle.h since the constructor from CCarton.h was made a friend function.do nothing if: The only reference to B is in a friend declaration
I figured that would apply to CCarton.h since the constructor accepts a reference to a CBottle object.forward declare B if: one or more functions has a B object/pointer/reference
as a parementer, or as a return type: B MyFunction(B myb);
Thanks for your time.