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, 01:13 AM
Authorized User
 
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
Default Chapter 7 SpreadSheet Error

Here is the error message I received when compiling the program:
Code:
1>------ Build started: Project: SpreadSheet, Configuration: Debug Win32 ------
1>  SpreadSheet.cpp
1>f:\professional c++\chapter 6\spreadsheet\spreadsheet\spreadsheet.cpp(32): error C2758: 'SpreadSheet::mTheApp' : a member of reference type must be initialized
1>          f:\professional c++\chapter 6\spreadsheet\spreadsheet\spreadsheet.h(27) : see declaration of 'SpreadSheet::mTheApp'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I have been trying to fix the error to no avail. Please help.

Here is my SpreadSheet.h code:
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;
};
and here is my SpreadSheet.cpp code:
Code:
// SpreadSheet.cpp

#include "SpreadSheet.h"

int SpreadSheet::sCounter = 0;

SpreadSheet::SpreadSheet(int inWidth, int inHeight, SpreadSheetApplication& theApp) : mWidth(mWidth < kMaxWidth ? inWidth : kMaxWidth),
	mHeight(inHeight < kMaxHeight ? inHeight : kMaxHeight), mTheApp(theApp)
{
	mId = ++sCounter;
	mCells = new SpreadSheetCell*[mWidth];
	for (int i = 0; i < mWidth; ++i) {
		mCells[i] = new SpreadSheetCell[mHeight];
	}
}

void SpreadSheet::copyFrom(const SpreadSheet& src)
{
	mWidth = src.mWidth;
	mHeight = src.mHeight;
	mCells = new SpreadSheetCell*[mWidth];

	for (int i = 0; i < mWidth; ++i)
		mCells[i] = new SpreadSheetCell[mHeight];

	for (int i = 0; i < mWidth; ++i)
		for (int j = 0; j < mHeight; ++j)
			mCells[i][j] = src.mCells[i][j];
}

SpreadSheet::SpreadSheet(const SpreadSheet& src)
{
	mId = sCounter++;
	copyFrom(src);
}

SpreadSheet& SpreadSheet::operator= (const SpreadSheet& rhs)
{
	// Check for self-assignment
	if (this == &rhs)
		return *this;

	// Free old memory
	for (int i = 0; i < mWidth; ++i)
		delete[] mCells;

	delete[] mCells;
	mCells = nullptr;

	// Copy new memory
	copyFrom(rhs);

	return *this;
}

SpreadSheet::~SpreadSheet()
{
	for (int i = 0; i < mWidth; i++)
		delete[] mCells[i];

	delete[] mCells;
	mCells = nullptr;
}

void SpreadSheet::setCellAt(int x, int y, const SpreadSheetCell& cell)
{
	if (!inRange(x, mWidth) || !inRange(y, mHeight)) {
		throw std::out_of_range("");
	}
	mCells[x][y] = cell;
}

SpreadSheetCell SpreadSheet::getCellAt(int x, int y)
{
	if (!inRange(x, mWidth) || !inRange(y, mHeight)) {
		throw std::out_of_range("");
	}
	return mCells[x][y];
}

bool SpreadSheet::inRange(int val, int upper)
{
	return (val <= mWidth && upper <= mHeight);
}

Last edited by phztfte1; April 16th, 2016 at 11:02 AM.. Reason: Put code in code block
 
Old April 17th, 2016, 01:42 PM
Wrox Author
 
Join Date: Mar 2011
Posts: 49
Thanks: 0
Thanked 4 Times in 4 Posts
Default

In your copy constructor you are missing an initialization of mTheApp. Should be:
Code:
Spreadsheet::Spreadsheet(const Spreadsheet& src) :
  mTheApp(src.mTheApp)
{
  mId = sCounter++;
  copyFrom(src);
}
 
Old April 17th, 2016, 06:17 PM
Authorized User
 
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
Default Chapter 7 SpreadSheet Error

Marc,

Thank you for finding my error. I missed the instruction of page 185 to also initialize the reference member in the copy construction.





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to extract a spreadsheet ravee39 SQL Server 2005 1 April 23rd, 2007 09:20 AM
Excel Spreadsheet Help SteveV BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 0 October 10th, 2006 04:41 PM
Excel Spreadsheet Help SteveV BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 0 October 10th, 2006 04:40 PM
CopyFromRecordset to Spreadsheet rweide SQL Server ASP 2 October 14th, 2004 03:56 PM
Transfer Spreadsheet JamesHardiman Pro VB Databases 0 November 26th, 2003 11:17 AM





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