Results 1 to 4 of 4

Thread: cannot access private member

  1. #1
    NOMADMAN
    Guest

    cannot access private member

    I have this definition in my template class:
    Code:
    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

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Are you sure it's a friend of the class grid?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    NOMADMAN
    Guest

    uhh i think so...

    Here is the entire grid.h file is it set up correctly?

    Code:
    #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

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Code:
    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);
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width