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);
}