p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5 (http://p2p.wrox.com/forumdisplay.php?f=394)
-   -   Chapter 9, Class Members as Friends (broken code) (http://p2p.wrox.com/showthread.php?t=86797)

deoren February 25th, 2012 11:51 AM

Chapter 9, Class Members as Friends (broken code)
 
The short version: the example code is broken and the errata for the book doesn't list this. The 2010 edition of the book goes into much more detail, but even that code is still broken.

The fix seems to be either:

Code:

#include carton.h
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:

Code:

friend CCarton;
when it should be (at least according to other resources):

Code:

friend class CCarton
Hope that helps someone.


All times are GMT -4. The time now is 07:56 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.