Wrox Programmer Forums
|
BOOK: Professional C++, 2nd Edition
This is the forum to discuss the Wrox book Professional C++, 2nd Edition by Marc Gregoire, Nicholas A. Solter, Scott J. Kleper ; ISBN: 978-1-1181-6995-7
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional C++, 2nd Edition 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 April 16th, 2016, 10:26 AM
Authorized User
 
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
Default Chapter 7 Friends, page 196

To make a specific member function of the Spreadsheet class a friend, the following statement was added to the SpreadsheetCell class:

Code:
friend void Spreadsheet::setCellAt(int x, int y, const SpreadSheetCell$ cell);
However, when doing so, there is a red squiggly line under Spreadsheet. Why so? Is it because Spreadsheet and SpreadsheetCell are not in the same namespace?

Last edited by phztfte1; April 16th, 2016 at 01:45 PM.. Reason: Corrected spelling error of squiggly
 
Old April 17th, 2016, 01:46 PM
Wrox Author
 
Join Date: Mar 2011
Posts: 49
Thanks: 0
Thanked 4 Times in 4 Posts
Default

In my Visual Studio (2015, Update 1), I don't see a red squiggle.
Note however that you have a small typo in your line. The correct line is:
Code:
friend void Spreadsheet::setCellAt(int x, int y, const SpreadsheetCell& cell);
 
Old April 17th, 2016, 06:53 PM
Authorized User
 
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
Default Chapter 7 Friends, page 196

Marc,

It appears that I made the typo when posting the code. The error message I get now is as follows:
Code:
1>  SpreadSheet.cpp
1>f:\professional c++\chapter 6\spreadsheet\spreadsheet\spreadsheetcell.h(10): error C2653: 'SpreadSheet' : is not a class or namespace name
1>  Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Here is the SpreadSheetCell.h file:
Code:
// SpreadSheetCell.h;

#include <string>
using std::string;

	class SpreadSheetCell
	{
	public:
		friend bool checkSpreadSheetCell(const SpreadSheetCell& cell);
		friend void SpreadSheet::setCellAt(int x, int y, const SpreadSheetCell& cell);
		SpreadSheetCell();										// default operator																				// default constructor
		SpreadSheetCell(double initialValue);					// single-argument constructor for double										// one argument constructor
		SpreadSheetCell(const string& initialValue);			// single-argument constructor for string
		SpreadSheetCell(const SpreadSheetCell& src);			// copy constructor
		SpreadSheetCell& SpreadSheetCell::operator= (const SpreadSheetCell& rhs);
		void setValue(double inValue);
		double getValue() const;
		void setString(const string& inString);
		string getString() const;
		typedef enum { Red = 1, Green, Blue, Yellow } Colors;
		void setColor(Colors color);

	protected:
		static string doubleToString(double inValue);
		static double stringToDouble(const string& inString);
		double mValue;
		string mString;
		mutable int mNumAccesses = 0;
		Colors mColor = Red;
	};
I made no changes to the SpreadSheetCell.cpp file because the method is defined in the SpreadSheet.cpp file.
 
Old April 18th, 2016, 02:13 PM
Wrox Author
 
Join Date: Mar 2011
Posts: 49
Thanks: 0
Thanked 4 Times in 4 Posts
Default

You are missing a:
Code:
#include "Spreadsheet.h"
or a forward declaration of Spreadsheet.
 
Old April 18th, 2016, 10:15 PM
Authorized User
 
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
Default Chapter 7 Friends, page 196

Marc,

When the statement
Code:
#include "Spreadsheet.h"
is added, a red squiggly line appears under #include with the error message: "Error: #include file "f:\professional c++\...\SpreadSheet.h" includes itself.

When I build a solution, the following error message is displayed:
Code:
1>------ Build started: Project: SpreadSheet, Configuration: Debug Win32 ------
1>  SpreadSheetCell.cpp
1>f:\professional c++\chapter 6\spreadsheet\spreadsheet\spreadsheet.h(3): fatal error C1014: too many include files : depth = 1024
1>  SpreadSheet.cpp
1>f:\professional c++\chapter 6\spreadsheet\spreadsheet\spreadsheetcell.h(3): fatal error C1014: too many include files : depth = 1024
1>  Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I suspect this error message is because of the SpreadSheet.h file which contains the statement
Code:
#include "SpreadSheetCell.h"
. See below.
Code:
// SpreadSheet.h

#include "SpreadSheetCell.h"

	class SpreadSheetApplication;		// forward declaration
	class SpreadSheet
	{
	public:
		SpreadSheet(int inWidth, int inHeight, SpreadSheetApplication& theApp);			// three-argument constructor
		SpreadSheet(const SpreadSheet& src);				// deep copy constructor
		SpreadSheet& operator= (const SpreadSheet& rhs);	// assignment operator
		void copyFrom(const SpreadSheet& src);			// helper method
		~SpreadSheet();										// destructor
		void setCellAt(int x, int y, const SpreadSheetCell& cell);
		SpreadSheetCell getCellAt(int x, int y);
		int getId() const;
		static const int kMaxHeight = 100;
		static const int kMaxWidth = 100;

	protected:
		static int sCounter;
		int mId;
		bool inRange(int val, int upper);	// protected helper method
		int mWidth, mHeight;		// data members
		SpreadSheetCell** mCells;	// SpreadSheetCell** used instead of a two-dimensional array 
		// to accommodate varying cell height and width
		SpreadSheetApplication& mTheApp;
	};
If I remove the
Code:
#include "SpreadSheetCell.h"
, red squiggly lines appear wherever "SpreadSheetCell" is stated.

What's going on?
 
Old April 20th, 2016, 03:36 PM
Wrox Author
 
Join Date: Mar 2011
Posts: 49
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Then you can probably use a forward declaration of Spreadsheet in SpreadSheetCell.h.

It's hard to follow what exactly you are trying to do.
Why don't you download the source code for the book from the Wiley/Wrox website?
All source code from the book is available for download and all that code is compilable without any changes.
 
Old April 20th, 2016, 11:20 PM
Authorized User
 
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
Default

Marc,

There was a beginning C++ book I read that said you could download the code or you could type in the code. If you type in the code, you will learn more because of the errors you will make. Resolving those errors will help you when you actually code a program.

I do peek at the downloaded source code to see if I am on track. There were a few differences. However, if I just change my code to be the same as the downloaded code without knowing why, I am not learning.

If I recall correctly, the book does not say what header files to incorporate into the ".cpp" file. You sort of have to figure it our.

What I am trying to do is find errors early. So, I compile the code at certain points, and if successful, the program is okay except for logical errors.

Since the code did not compile, I know something is wrong, but have yet to figure it out. That is why I am asking for help.

What is a forward declaration of SpreadSheet?
 
Old April 21st, 2016, 01:26 PM
Wrox Author
 
Join Date: Mar 2011
Posts: 49
Thanks: 0
Thanked 4 Times in 4 Posts
Default

That is correct. That way you learn more. I just wanted to make sure you knew that you can download the source code for this book and look at it.

Forward declarations are briefly discussed and demonstrated in Chapter 7 on page 184 (second edition of the book) section "Reference Data Members".
 
Old April 21st, 2016, 10:26 PM
Authorized User
 
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
Default Chapter 7 Friends, page 196

Marc,

I found the solution. I added the preprocessor directives for each header file although it is only required for the SpreadsheetCell.h file.
Code:
#ifndef _XXXXXX_XXX_H_
#define _XXXXXX_XXX_H_
...
#endif
The code compiles.
 
Old April 26th, 2016, 02:02 PM
Wrox Author
 
Join Date: Mar 2011
Posts: 49
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Glad you found the solution.
Well done :)





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++ 2010 0 February 25th, 2012 12:09 PM
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
Hi,Friends emmanuelmosses Crystal Reports 1 August 25th, 2008 12:38 PM
Beginning ASP.NET 2.0 - Chapter 6 - page 196 warren_banks BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 0 September 2nd, 2006 08:21 AM
Chapter 5 page 196- duplicating objects sasman23x BOOK: Beginning Java 2 1 January 31st, 2004 09:09 AM





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