The short version: the example code is broken and the errata for the book doesn't list this. This edition of the book goes into much more detail than the 2008 edition, but even that code is still broken if you don't adhere to a strict order of #include statements in a main .cpp file.
Here is an example main .cpp file called
class_mem_as_friends.cpp (not the best name, but it works):
Code:
#include "CCarton.h"
#include "CBottle.h"
int main ()
{
CBottle myBottle(2, 3);
CCarton myCarton(myBottle);
return 0;
}
If you want the order of include statements to be interchangeable (like most header files are), then it appears you'll need to do one of either:
inside of bottle.h or
Code:
friend class CCarton
inside of the CBottle class.
This looks like:
Code:
#pragma once
#include "carton.h"
class CBottle
{
public:
CBottle(double height, double diameter);
private:
double m_Height; // Bottle height
double m_Diameter; // Bottle diameter
// Let the carton constructor in
friend CCarton::CCarton(const CBottle& aBottle);
};
or
Code:
#pragma once
class CBottle
{
friend class CCarton;
public:
CBottle(double height, double diameter);
private:
double m_Height; // Bottle height
double m_Diameter; // Bottle diameter
};
respectively.
More info available on the cboard forums
here.
P.S.
Both page 530 (2008 edition) and page 571 (2010 edition) have the syntax for adding a friend declaration within the CBottle class definition wrong:
when it should be (at least according to other resources):
Code:
friend class CCarton
Hope that helps someone.
P.P.S.
This is nearly a duplicate of another post
here, but I'm posting it in both forums since it applies to both editions of the book.