2016-12-03 59 views
-1

所以首先我不知道我是否宣佈它是正確的,或者正確地初始化它。在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)並列出設置方向。希望這不是一個簡單的愚蠢的答案,但因爲我不知道請告訴:)

+0

移動你的枚舉定義的函數體之外 – Eugene

回答

0

當讀取提供的源代碼時,我可以看到變量和指針(enum D_direction *directionshould the argument be bool *pos?)之間的一些搖擺。

需要明確的是,所有簡單的變量類型爲intfloat,...和 enum可以輸入或從功能而不需要 指針返回。

要在課堂上使用的枚舉,它是非常簡單的:

第一步 - 與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的私有數據和 get/set公共函數。

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

第二步 - 使用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; 
} 

第三步: - 然後用enumclass

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

謝謝!我的教授建議讓它成爲一個全局變量,但是我仍然對get函數有問題......儘管作爲一個全局變量可能沒有必要......呃......我可以說我在學習什麼。我沒有考慮使用枚舉變量來設置rand()%的參數。所以再次感謝,我會試試這個。 –

+0

實際上,** Step3 **被描述爲插入到你的'main()'函數或任何其他內部。 –

+0

所以再次閱讀你的評論,我發現你和我的教授一樣......哎呀。 :D –