Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 用细胞表示迷宫_C++ - Fatal编程技术网

C++ 用细胞表示迷宫

C++ 用细胞表示迷宫,c++,C++,我试图表示迷宫对象,它是一个矩形网格单元,即一个二维单元对象数组。但我不明白为什么我不能这样设置 m[1][1].setDown(false); 我将得到错误“arr是一个私有成员”,但我在这里对cell对象使用settermethod,所以它不应该工作吗?有没有其他更好的方法来表示迷宫中的细胞对象 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include<fstream> #include<st

我试图表示迷宫对象,它是一个矩形网格单元,即一个二维单元对象数组。但我不明白为什么我不能这样设置

 m[1][1].setDown(false); 
我将得到错误“arr是一个私有成员”,但我在这里对cell对象使用settermethod,所以它不应该工作吗?有没有其他更好的方法来表示迷宫中的细胞对象

#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);
}
\define\u CRT\u SECURE\u NO\u警告
#包括
#包括
#包括
#包括
使用名称空间std;
类单元
{
私人:
布尔访问;
向上,向下,向右,向左;
int x,y;
int-px,py;
字符状态;
公众:
单元格(){
向上=向下=右=左=0;
x=y=0;
状态='';
}
bool getUp(){return up;}
bool getDown(){return down;}
bool getRight(){return right;}
bool getLeft(){return left;}
bool getvisited(){返回已访问;}
无效设置(bool b){up=b;}
无效设置(bool b){down=b;}
void setRight(bool b){right=b;}
void setLeft(bool b){left=b;}
void setvisted(bool b=0){visted=b;}
void setstatus(char s=''){status=s;}
char getStatus(){return status;}
};
班级迷宫
{
私人:
int row,col;
单元**arr;
公众:
迷宫(int r=0,int c=0)
{
此->行=r;此->列=c;
arr=新单元*[行];
对于(int i=0;i
当然可以。这是意料之中的arr在类的私有部分中定义,因此类外的任何内容都无法访问它

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

要么将其公开,要么通过某种api封装对其的访问。

一种解决方案是通过以下更改迷宫公开声明来表示迷宫对象的更好方法:

public: 
Cell GetCell(int r,int c) {
     return arr[r][c];
}
然后在主方法中将错误行更改为:

m.GetCell(1,1).setLeft(true);

setter是公共的,但
arr
不是。请注意
Maze
容易被复制、分配和泄漏。您应该阅读。警告:
使用名称空间std可能会给您带来一些悲伤。或者不是。弗朗索瓦·安德烈指出,解决问题,问题就会消失。我该如何解决?通过公开?另一种可能是将单元格的x/y坐标传递给设置者。@kogmaw1337您可以公开,但一些傻瓜可以轻松地
删除arr。那太糟糕了。您可能希望创建一个访问器函数,该函数接受行和列,并返回对要修改的元素的引用。@user4581301由于
Maze
类不能处理自己的资源,因此将
arr
公开可能是正确使用此类型的唯一方法。虽然这只是在解决真正的问题。