最初に正しいと宣言したのか、正しく初期化したのか分かりません。 DOCO.hでは、enumとして宣言し、変数getおよびsetを作成しました。変数がこのクラスでのみ使用されていることがどれほど必要なのかわかりませんが、それ以降のバージョンではサブクラスでその変数にアクセスする必要があるでしょうか?クラス内でEnum変数を取得して設定しますか?
オブジェクトDOCOは2D配列に沿って移動していますので、DOCOにenum変数を持たせることで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 *方向の変数を使用していた1つの方法です。私は静的キャストを正しく行っているとは思わない。
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には多くのエラーがあります。私は本当に何を尋ねるのかはわかりませんが、私の質問に関連していると思います。私はグリッド内のブロックの周りのDOCOの方向を変更するためにenum変数を使いたいと思います。私は行のある方向として、上向き(NW)、上向き(N)、上向き(NE)、右向き(E)、下向き(SE)、下向き(S)、下向き(SW)および右向き(W)方向を設定するにはcolを使用します。うまくいけば、これは簡単な愚かな答えではありませんが、わからないので、教えてください:)
は、関数本体の外側で – Eugene