2016-12-03 51 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 –