当我运行5次示威功能时,我一无所获
因此,当我运行这个程序时,
#include<iostream>
#include<ctime>
float ex, ey;
class Enemy
{
public:
float x, y;
Enemy()
{
x = ex;
y = ey;
}
void showLocation()
{
std::cout<<x<<" , "<<y<<std::endl;
}
};
int main()
{
Enemy e;
for(int i = 0;i<5,i++;)
{
ex = rand() % 5 + 1;
ey = rand() % 5 + 1;
e.showLocation();
}
}
我什么都没有,我做错了什么? 我有一个空白的空间,然后说:“流程退出 - 返回代码0”
so when i run this program,
#include<iostream>
#include<ctime>
float ex, ey;
class Enemy
{
public:
float x, y;
Enemy()
{
x = ex;
y = ey;
}
void showLocation()
{
std::cout<<x<<" , "<<y<<std::endl;
}
};
int main()
{
Enemy e;
for(int i = 0;i<5,i++;)
{
ex = rand() % 5 + 1;
ey = rand() % 5 + 1;
e.showLocation();
}
}
I get nothing, am I doing something wrong?
I get a blank space, then it says, "Process exited - return code 0"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对象
e
是在for循环之前初始化的。因此,其数据成员x
和y
的设置等于0.0F
(全局变量ex
和ey
是由默认构造函数隐式初始化的,并且不再更改。您需要在for循环中声明对象,例如,
在这种情况下,在for循环的每次迭代中,将创建一个新对象
e
数据成员将通过其计算值来初始化全局变量ex
和ey
。The object
e
is initialized before the for loop. So its data membersx
andy
are set equal to0.0f
(the global variablesex
andey
are implicitly zero initialized) by the default constructor and are not changed any more.You need to declare the object in the for loop for example like
In this case in each iteration of the for loop there will be created a new object
e
data members of which will be initialized by calculated values of the global variablesex
andey
.