Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > BOOK: Ivor Horton's Beginning Visual C++ 2010
|
BOOK: Ivor Horton's Beginning Visual C++ 2010
This is the forum to discuss the Wrox book Ivor Horton's Beginning Visual C++ 2010 by Ivor Horton; ISBN: 9780470500880
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Ivor Horton's Beginning Visual C++ 2010 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 25th, 2012, 12:09 PM
Registered User
 
Join Date: Feb 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation 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. 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:

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.

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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 9, Class Members as Friends (broken code) deoren BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5 0 February 25th, 2012 11:51 AM
using members of a C Sharp class Bill_Thompson C# 2005 4 January 13th, 2009 06:04 PM
ZIP with code is broken! CROmagnon BOOK Beginning CSS: Cascading Style Sheets for Web Design, 2nd Ed; ISBN: 978-0-470-09697-0 2 May 23rd, 2008 11:28 AM
hai friends, Regarding Client-side code. sridharmohan15 ASP.NET 1.0 and 1.1 Professional 2 November 27th, 2006 08:19 AM





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