2016-12-03 4 views
-1

最初に正しいと宣言したのか、正しく初期化したのか分かりません。 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を使用します。うまくいけば、これは簡単な愚かな答えではありませんが、わからないので、教えてください:)

+0

は、関数本体の外側で – Eugene

答えて

0

提供されたソースコードを読むと、変数とポインタ(enum D_direction *directionshould the argument be bool *pos?)の間に何らかの揺れが見えます。

を明確にするために、すべての単純な変数の型は intfloat、...と enumように入力することができまたは必要に応じ ポインタなし関数から返されました。

クラスで列挙型を使用するには、それは本当に簡単です:

ステップ1からclassenumの宣言:

enumは宣言記号パラメータのセットです。使用前に

enum D_direction { D_None, 
     D_NW, D_N, D_NE, D_E, 
     D_SE, D_S, D_SW, D_W 
    }; 

classでは、enumは、intとしてプライベートデータを使用し、 パブリック関数を取得/設定します。

class DOCO { 
private: 
    enum D_direction e_dir; 
public: 
    DOCO(); 
    enum D_direction getD_direction(); 
    void setD_direction(enum D_direction dir); 
}; 

ステップ2 - 自然な変数の型としてenumを使用してください。

DOCO::DOCO() 
{ 
    e_dir = D_None; 
} 

enum D_direction DOCO::getD_direction() 
{ 
    return (e_dir); 
} 

void DOCO::setD_direction(enum D_direction dir) 
{ 
    e_dir = dir; 
} 

ステップ3 - そして、あなたのclassenumを使用します。

DOCO vDoco; 
enum D_direction e_mydir; 

e_mydir = static_cast<enum D_direction>(rand()%(D_W-D_NW)+D_NW); //8+1); 
vDoco.setD_direction(e_mydir); 
+0

感謝をあなたの列挙型定義を移動します!私の教授は、それがうまくいったグローバル変数にすることを提案しましたが、私はまだ関数を取得することに問題があります...グローバルではおそらく必要ではないのですが...ええと...私は何を学んでいると言えますか? enum変数を使ってrand()%のパラメータを設定することは考えていませんでした。だからもう一度感謝、私はこれを試してみます。 –

+0

実際、** Step3 **は、あなたの 'main()'関数または他のものの中に挿入されると記述されていました。 –

+0

あなたのコメントをもう一度読んで、あなたが私の教授と同じことを言っていたことが分かりました。 :D –

関連する問題