兰德函数不"defined"
rand function not "defined"
我目前正在使用 Visual Studio 用 c++ 编写我的第一个游戏(贪吃蛇),我正在尝试在屏幕上的随机区域设置你吃的块,但是当我使用 rand() 函数时,它说它是未定义的,有人知道为什么我有这个错误吗?
#include <iostream>
using namespace std;
bool gameOver;
const int WIDTH = 20;
const int HEIGHT = 20;
int x, y, foodX, foodY, score;
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN};
eDirection dir;
void Setup(){
gameOver = false;
dir = STOP;
x = WIDTH /2;
y = HEIGHT /2;
//rand function below is not defined??
//I thought the function was built in
foodX = rand() % WIDTH;
foodY = rand() % HEIGHT;
}
void Draw() {
}
void Input() {
}
void Logic() {
}
int main()
{
Setup();
while (!gameOver)
{
Draw();
Input();
Logic();
}
return 0;
}
您缺少包含
#include <cstdlib>
http://www.cplusplus.com/reference/cstdlib/rand/
> rand
是在标头cstdlib
中定义的,因此您需要添加以下内容:
#include <cstdlib>
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 如何将指向 C 中函数的"DEFINED"常量的指针传递给函数?
- 函数模板不起作用,出现错误"No suitable user-defined conversion"
- CGAL 绘制函数在 Visual Studio 中给出'CGAL_USE_BASIC_VIEWER is not defined'错误
- #defined 函数只能接受原始输入而不接受变量?
- 全局静态变量不"staying defined"函数外部
- "Destructor already defined"专用析构函数
- 可点击的QLabel类"multiply defined"函数
- LNK2005 .obj 中的函数指针数组"already defined"
- 兰德函数不"defined"