PDA

Click to See Complete Forum and Search --> : cannot access private member


NOMADMAN
Apr 9th, 2002, 02:14 AM
I have this definition in my template class:

template <class cell>
ostream & operator << (ostream & o, const grid<cell> g)
{
for (unsigned short r = 0; r < g.maxrows; r++)
{
for (unsigned short c = 0; c < g.maxcols; c++)
{
o << g.lattice[r][c];
}
o << endl;
}
return o;
}


maxrows is in the protected section of class decleration. I'm assuming I get the Can not access private members because I used g.maxrows(cols) and g.lattice. But if I take the g. off it says undeclared. This is cause its a friend and not a direct member of the class?

What can I do to get around this? I need to use maxrows and maxcols of the grid type and output info in lattice[r][c].

NOMAD

CornedBee
Apr 9th, 2002, 08:04 AM
Are you sure it's a friend of the class grid?

NOMADMAN
Apr 9th, 2002, 12:09 PM
Here is the entire grid.h file is it set up correctly?


#ifndef GRID_H
#define GRID_H

#include <iostream>
using std:: ostream;
using std::endl;

template <class cell>
class grid
{
protected:
cell * * lattice;
unsigned short maxrows;
unsigned short maxcols;
public:
grid();
grid(cell initalvalue, unsigned short rows,
unsigned short cols );
grid(grid & g);

bool changerow(cell newvalue, unsigned int rownumber);
bool changecolumn(cell newvalue, unsigned int columnnumber);
void changegrid(cell newvalue);

ostream & operator << (ostream & o, const grid<cell> g);
~grid();
};

// DEFINITIONS
template <class cell>
grid<cell>::grid()
{
maxrows = 1;
maxcols = 1;
lattice = new cell * [l];
lattice[0] = new cell [l];
}
///////////////////////////////
template <class cell>
grid<cell>::grid(cell inialvalue, unsigned short rows,
unsigned short cols)
{
maxrows = rows;
maxcols = cols;
lattice = new cell * [rows];
for (int r = 0; r < rows; r++)
{
lattice[r][c] = initialvalue;
}
}
////////////////////////////////
template <class cell>
grid<cell>::grid(grid & g)
{
maxrows = g.maxrows;
maxcols = g.maxcols;
lattice = new cell * [maxrows];
for (int r = 0; r < maxrows; r++)
{
lattice[r] = new cell [maxcols];
for ( int c = 0; c < maxcols; c++)
{
lattice[r] [c] = g.lattice[r] [c];
}
}
}
////////////////////////////////
template <class cell>
bool grid<cell>::changerow(cell newvalue, unsigned int rownumber)
{
if (rownumber < maxrows)
{
for (int c = 0; c < maxcols; c++)
lattice[rownumber][c] = newvalue;
return true;
}
else
return false;
}
////////////////////////////////
template <class cell>
bool grid<cell>::changecolumn(cell newvalue, unsigned int columnnumber)
{
if (colnumber < maxcols)
{
for (int r = 0; r < maxrows; r++)
lattice[r][colnumber] = newvalue;
return true;
}
else
return false;
}
////////////////////////////////
template <class cell>
void grid<cell>::changegrid(cell newvalue)
{
for (int r = 0; r < maxrows; r++)
{
for (int c = 0; c < maxcols; c++)
{
lattice[r][c] = newvalue;
}
}
}
////////////////////////////////
template <class cell>
ostream & operator << (ostream & o, const grid<cell> g)
{
for (unsigned short r = 0; r < g.maxrows; r++)
{
for (unsigned short c = 0; c < g.maxcols; c++)
{
o << g.lattice[r][c];
}
o << endl;
}
return o;
}

////////////////////////////////
template <class cell>
grid<cell>::~grid()
{
for (int r = 0; r < maxrows; r++)
{
delete [] lattice[r];
}
delete [] lattice;
}
//END GRID.H

#endif

CornedBee
Apr 10th, 2002, 07:49 AM
template <class cell>
class grid
{
protected:
cell * * lattice;
unsigned short maxrows;
unsigned short maxcols;
public:
grid();
grid(cell initalvalue, unsigned short rows,
unsigned short cols );
grid(grid & g);

bool changerow(cell newvalue, unsigned int rownumber);
bool changecolumn(cell newvalue, unsigned int columnnumber);
void changegrid(cell newvalue);

ostream & operator << (ostream & o, const grid<cell> g);
~grid();
};



The highlighted line must include the keyword friend. I'm not sure whether it needs it's own template declaration.

I think the correct way would be
friend ostream & operator << (ostream & o, const grid<cell> g);