将结构的枚举传递给其他功能并分配值
Passing an enum of a structure to other functions and assigning the values
我正在用C 编写蛇游戏,我有一个蛇的结构,其中包含数据,例如x位置,y位置,方向等。
我都可以正常工作,将所有数据设置为整数,我只是想将某些数据类型更改为Enum的数据,因为它看起来很整洁,更易于理解。我尝试了很多东西,在网上看,但我似乎找不到任何东西。
这是一些结构:
struct SnakeSection
{
int snakePosX;
int snakePosY;
int SectionType;
// Tail = 0, Body = 1, Head = 2
int animation;
enum Direction
{
Up = 0,
Right = 1,
Down = 2,
Left = 3
};
};
我试图尝试将一个方向之一传递到另一个功能的尝试:
void PlayerSnake::createSnake()
{
// Parameters are direction, x and y pos, the blocks are 32x32
addSection(SnakeSection::Direction::Right, mStartX, mStartY, 2);
}
然后,我尝试将方向设置为在该功能中传递的方向:
void PlayerSnake::addSection(SnakeSection::Direction dir, int x, int y, int type)
{
//Create a temp variable of a Snake part structure
SnakeSection bufferSnake;
bufferSnake.Direction = dir;
bufferSnake.animation = 0;
//is it head tail or what? This is stored in the Snake section struct
//TODO Add different sprites for each section
bufferSnake.SectionType = type;
//assign the x and y position parameters to the snake section struct buffer
bufferSnake.snakePosX = x;
bufferSnake.snakePosY = y;
//Push the new section to the back of the snake.
lSnake.push_back(bufferSnake);
}
错误:无效使用枚举蛇:: Direction
谢谢
以下行上的错误...
bufferSnake.Direction = dir;
...是有理由的,除了声明enum
类型外,您仍然必须拥有一个类成员变量来存储它:
struct SnakeSection
{
int snakePosX;
int snakePosY;
int SectionType;
// Tail = 0, Body = 1, Head = 2
int animation;
enum Direction
{
Up = 0,
Right = 1,
Down = 2,
Left = 3
};
Direction direction_; // <<<<<<<<<<<<<< THAT'S WHAT'S MISSING IN YOUR CODE
};
并参考
bufferSnake.direction_= dir; // <<<<<<<<<<<<<< THAT'S THE MEMBER VARIABLE YOU'LL
// HAVE TO REFER TO!
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 如何从其他功能C++访问参数?
- 如何使用 C/C++ 和 system() 系统调用以外的其他方法在 Linux 中获取文件功能?
- char* 除了作为C++中的指针之外,还有其他功能吗?
- 如何让线程继续并启动其他功能 - C++使用 OpenMP?
- 如果我具有调用其其他实例之一的超载函数,它是否被认为是递归功能
- 如何使用使用代替定义或其他C++功能
- 在 SCIP 中集成来自其他源代码的功能
- 传递多个参数的功能,该函数将类包含到其他函数
- 从Baseclass获取其他功能
- 仅一个功能的链接错误.在同一类和CPP文件中具有相似签名的其他函数工作正常
- Vigenere密码的C++功能有时才起作用(适用于某些输入,跳过其他输入的班次)
- 如何与 PlaySound 同时执行其他功能?
- 在递归功能中,我如何跳到堆栈上的其他功能调用
- 在Google测试框架中,如何期望函数调用或其他功能调用
- 功能模板与Variadic模板过载:Intel C 编译器版本18产生的结果不同于其他编译器.英特尔是错误的
- 我想制作一个程序来执行一些用户制作的功能,例如加法和其他东西
- 您如何共享Java功能而不提供其他源代码
- 无法从类中的其他功能访问同一类的变量
- 模板功能以映射特定类型,并保留所有其他不变的