0

I am trying to represent the Maze object which is a rectangular grid of cells i.e., a 2-dimensional array of Cell objects. But i am confused why i cant set it this way

 m[1][1].setDown(false); 

i will get the error "arr is a private member", but i am here using a settermethod on the cell object, so shouldnt it work? Is there another better way to represent the Cell object inside the maze?

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<fstream>
#include<string>
#include <stack>
using namespace std;

class cell
{
private:
    bool visited;
    bool up, down, right, left;
    int x, y;
    int px, py;
    char status;
public:
    cell() {
        up = down = right = left = 0;
        x = y = 0;
        status = ' ';
    }
    bool getUp() { return up; }
    bool getDown() { return down; }
    bool getRight() { return right;  }
    bool getLeft() { return left; }
    bool getvisited() { return visited; }
    void setUp(bool b) { up = b; }
    void setDown(bool b) { down = b; }
    void setRight(bool b) { right = b; }
    void setLeft(bool b) { left = b; }

    void setvisited(bool b=0) { visited = b; }
    void setstatus(char s = ' ') { status = s; }
    char getStatus() { return status; }

};

class Maze
{
private:
    int row, col;
    cell **arr;
public:

    Maze(int r = 0, int c = 0)
    {
        this->row = r; this->col = c;
        arr = new cell*[row];
        for (int i = 0; i < row; i++)
            arr[i] = new cell[col];
    }


};

int main()
{

    Maze m(5, 5);

    m.arr[1][1].setLeft(true);
}

2 Answers2

0

Sure. That is to be expected. arr is defined in a private part of the class, so nothing outside the class can access it.

private:
    int row, col;
    cell **arr;
public:

Either make it public, or encapsulate access to it through some kind of api.

EvilTeach
  • 28,120
  • 21
  • 85
  • 141
  • How can i fix it ? by making it public? – kogmaw1337 Oct 21 '19 at 17:59
  • Another possibility would be to pass in the x/y coordinate of the cell to the setters. – EvilTeach Oct 21 '19 at 18:01
  • @kogmaw1337 You could make it public, but then some fool could easily `delete arr;`. That would be bad. You may want to make an accessor function that takes row and column and returns a reference to the element you wish to modify. – user4581301 Oct 21 '19 at 18:01
  • [Here is a good discussion on making safe matrix classes](https://isocpp.org/wiki/faq/operator-overloading#matrix-subscript-op) – user4581301 Oct 21 '19 at 18:03
  • @user4581301 Seeing as the `Maze` class doesn't handle it's own resources, having `arr` be public may be the *only* way to correctly use this type. Though that's just working around the real problem. – François Andrieux Oct 21 '19 at 18:03
  • [This is my go-to Stack Overflow link for simple, safe, and effective matrixes](https://stackoverflow.com/a/2076668/4581301) – user4581301 Oct 21 '19 at 18:05
  • should i make cell to be a friend class of maze? – kogmaw1337 Oct 21 '19 at 18:34
0

One solution is a better way to represent your maze object by altering your Maze public declaration with this:

public: 
Cell GetCell(int r,int c) {
     return arr[r][c];
}

Then in your main method alter the erroring line to this:

m.GetCell(1,1).setLeft(true);
tkefauver
  • 491
  • 5
  • 19