所以首先我不知道我是否宣佈它是正確的,或者正確地初始化它。在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