所以首先我不知道我是否宣布它是正确的,或者正确地初始化它。在DOCO.h中,我将它声明为一个枚举,然后创建了一个变量get和set。我不知道它是多么的必要,因为这个变量只能在这个类中使用,但是后面的版本可能需要在子类中访问它。获取并设置类内的枚举变量?
因此,对象DOCO正在沿着2D数组移动,我希望能够通过允许它具有枚举变量来改变DOCO的方向。首先,作为编码人员,我知道我在谈论如何将其设置为北,南,东,西等名称,但允许程序将其转换为整数值。
DOCO.h
{ 私人: ... 枚举D_direction;
public:
...
enum D_direction getD_direction();
void setD_direction(enum D_direction dir);
}
DOCO.cpp
DOCO::DOCO(void)
{
enum D_direction *direction = 0;
D_energy = 0;
Mycell = NULL;
Nextcell = NULL;
}
enum D_direction DOCO::getD_direction()
{
enum D_direction dir;
//possible returns------
return *dir;
/*ERROR: no "*" matches these operands
operand types are: *DOCO::D_direction */
return D_direction *dir
/*enum DOCO::D_direction, DOCO *Other_proK;
ERROR: typename is not allowed*/
return dir;
/* DOCO::D_direction dir
ERROR: the return value type does not match the function type*/
}
void DOCO::setD_direction(enum D_direction dir)
{
enum D_direction {No_dir,NW,N,NE,E,SE,S,SW,W};
dir = static_cast<enum D_direction>(rand()%8+1);
//ERROR: a value type of "D_direction" can't be assigned to an
//entity of "DOCO::D_direction"
}
所以这个功能是不完全正确的,但是这是我想用枚举D_direction *方向变化的一种方式。我不认为我正在进行正确的静态投射。
bool DOCO::checkPosition(W_Cell *mc)
//should the argument be bool *pos? Don't know if this needs to be used?
{
//use the enum D_direction and pointer to next cell and determine
//current cell is border cell and move is possible
bool pos = false;
//do I need to instantiate the W_Grid?
while (row != -1 && col != -1)
{
if (*direction == NW)
{
if ((col == 0) || (row == 0)) //top row or 1st col no NW
pos = false;
else
pos = (D_World)grid[row-1][col-1].isDoccupied();
//can I set all of these true?
}
else if (*direction == N)
{
if (row == 0) //top row no N nextcell
pos = false;
else
pos = (D_World)grid[row-1][col+1].isDoccupied();
}
else if (*direction == NE)
{
if ((row == 0) || (col = width - 1)
//top row or last col no NE nextcell
pos = false;
else
pos = D_World::grid[row-1][col+1].getDoccupied();
}
else if (*direction == E)
{
if (col = width - 1) //last col no E nextcell
pos = false;
else
pos = (D_World)grid[row][col+1].isDoccupied();
}
else if (*direction == SE)
{
if ((row == (height -1))||col == (width - 1)))
//last row or col no SE nextcell
pos = false;
else
pos = (D_World)grid[row+1][col+1].isDoccupied();
//or can I say this is setNextcell(dOC);
}
else if (*direction == S)
{
if ((row == (height -1)) //last row no S nextcell
pos = false;
else
pos = (D_World)grid[row+1][col].isDoccupied();
//or can I say this is setNextcell(dOC);
}
else if (*direction == SW)
{
if ((row == (height -1) || (col == 0))
//last row or 1st col no SW nextcell
pos = false;
else
pos = (D_World)grid[row+1][col].isDoccupied();
//or can I say this is setNextcell(dOC);
}
else if (*direction == W)
{
if (col = 0) //1st col no W nextcell
pos = false;
else
pos = (D_World)grid[row][col-1].getDoccupied();
}
else
pos = false;
}
return pos;
}
该函数显示了我想如何使用这个变量。我没有任何错误调用变量,但我不知道这是否正确使用它。 void DOCO :: setNextcell(W_Cell * nc) { W_Cell * Nextcell = new W_Cell; D_World * grid = new D_World;
if (Nextcell= grid-> getCell(row, col))
{
if (Nextcell -> getP_occupied())
/*how to get pointer to where cell is on grid?*/
this -> checkPosition(nc);
/*so this will point to grid->getCell(row,col)?*/
if (Nextcell->getD_occupied())
this -> getD_direction;
}
Nextcell = nc;
}
所以这里是我认为我得到这个错误。在DOCO.h中,我没有收到任何错误。但是在DOCO.cpp中有很多错误。我不知道该问什么,但这是我认为与我的问题相关的内容。我想使用enum变量来改变网格中块的DOCO方向。我将沿着我的方向使用左上(NW),上(N),右上(NE),右(E),右下(SE),下(S),左下(SW)和右(W)并列出设置方向。希望这不是一个简单的愚蠢的答案,但因为我不知道请告诉:)
移动你的枚举定义的函数体之外 – Eugene