Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.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++_Arrays - Fatal编程技术网

C++ 检查二维数组中的对象是否为空

C++ 检查二维数组中的对象是否为空,c++,arrays,C++,Arrays,我有一个二维数组,用来容纳不断移动的物体 class ZombieLand : public Singleton<ZombieLand> { DECLARE_SINGLETON(ZombieLand); public: MachineState world [19][19]; MachineState getWorld() { std::cout<<"World"; return world[19][19];

我有一个二维数组,用来容纳不断移动的物体

class ZombieLand : public Singleton<ZombieLand>
{
   DECLARE_SINGLETON(ZombieLand);
public:
    MachineState world [19][19];
    MachineState getWorld()
    {
       std::cout<<"World";
       return world[19][19];
    }
    void setWorld(MachineState & state)
    {
    world [state.x][state.y] = state;

    }
}
我的问题是,如何检查我的世界数组的某个位置是否已包含对象?先谢谢你

我的地产课

struct MachineState
{

template <typename MachineTraits>
friend class Machine;

enum Facing { UP, RIGHT, DOWN, LEFT};
MachineState()
    : m_ProgramCounter(1)
    , m_ActionsTaken(0)
    , m_Facing(UP)
    , m_Test(false)
    , m_Memory(nullptr)
    ,x(0)
    ,y(0)
    ,point1(25, 10)
    ,point2(10, 40)
    ,point3(40, 40)

{ }
  int m_ProgramCounter;
  int m_ActionsTaken;
  Facing m_Facing;
  bool m_Test;
  bool m_occupied;
  int x;
  int y;
  Point point1;
  Point point2;
  Point point3;

  int GetActionsPerTurn() const throw() { return m_ActionsPerTurn; }
  int GetMaxMemory() const throw() {return m_MaxMemory; }
  bool GetInfect() const throw() { return m_InfectOnAttack; }
  void setPoint(Point p1, Point p2, Point p3)
  {
    point1=p1;
    point2=p2;
    point3=p3;
 }
struct-MachineState
{
模板
朋友级机器;
枚举面向{上、右、下、左};
房地产()
:m_程序计数器(1)
,m_ActionsTaken(0)
,m_面朝上(向上)
,m_检验(假)
,m_内存(空PTR)
,x(0)
,y(0)
,第1点(25,10)
,第2点(10,40)
,第3点(40,40)
{ }
int m_程序计数器;
国际行动站;
面朝m_面朝;
布尔m_检验;
布尔穆被占领;
int x;
int-y;
第1点;
第2点;
第3点;
int GetActionsPerTurn()常量throw(){return m_ActionsPerTurn;}
int GetMaxMemory()常量throw(){return m_MaxMemory;}
bool GetInfect()const throw(){return m_InfectOnAttack;}
无效设定点(点p1、点p2、点p3)
{
点1=p1;
点2=p2;
点3=p3;
}

})

你有两条路可以走

首先:在
MachineState
的构造函数中,必须设置一个变量,该变量确定该字段上的内容,并添加一个函数,该函数告诉您这一点。如果该字段的返回值为“0”,则您将知道该字段为空

第二:声明一个二维指针数组

MachineState *world[19][19];
memset(world, 0, sizeof(world));
这段小代码将所有指针设置为0(NULL),然后您可以检查它。当然,您需要手动将对象分配给这个二维数组

另外,您的
getWorld()
函数看起来有点问题

请考虑使用这个函数代替:

MachineState *getField(MachineState state) {
    return world[state.x][state.y];
}
或者,您当然可以使用:

MachineState *getField(int X, int Y) {
    return world[X][Y];
}
后一个函数可以称为:

MachineState *Field = ZombieLand::getField(state.x, state.y - 1);
if(Field != NULL) state.m_occupied = true;

当然,您需要添加边界检查,但这超出了本答案的范围。

如何定义
MachineState
?您是否考虑过创建指向类型为
MachineState
的对象的
指针的二维数组bad@Dave谢谢,我只是在看这本书。它说这比声明全局变量要好,所以我尝试实现itHey@Refugnic Eternium。首先谢谢你的帮助。第二,我已经添加了我的机器类,以防您需要查看它……但看起来您的解释起了作用
MachineState *Field = ZombieLand::getField(state.x, state.y - 1);
if(Field != NULL) state.m_occupied = true;