【发布时间】:2014-01-11 23:59:07
【问题描述】:
我正在为一个大学项目设计一个机器人模拟器,但在一些碰撞检测方面遇到了一个大问题。这是我的robot.h头文件:
#ifndef robot_h
#define robot_h
#include <vector>
enum direction
{
UP,DOWN,LEFT,RIGHT
};
enum motor
{
STOP,SLOW,FAST
};
class robot
{
public:
robot();
char bot; // The bot onscreen
int getX(); // The X position of robot
int getY(); // The Y position of robot
int dir; // The direction the robot is going
bool touchSensor; // Boolean value if collision
int lightSensor; // light sensor between 10-100
int motorA; // Motor A between 0, 1 and 2
int motorB; // Motor A between 0, 1 and 2
void detection(int x, int y);
void getReturnObject();
bool returnObjectDash;
bool returnObjectLine;
void move(); // Moving the robot
void draw(); // Drawing the robot on screen
void update(); // Updating the robot position
private:
int positionX; // Starting X value
int positionY; // Starting Y value
};
#endif
基本上,我使用了两个布尔值:
returnObjectDash;和returnObjectLine。我的 matrix.cpp 文件中有这段代码:
void matrix::detection(int x, int y)
{
if(vector2D[x][y]=='-')
{
returnObjectDash=true;
system("pause");
}
else
{
returnObjectDash=false;
}
if(vector2D[x][y]=='|')
{
returnObjectLine=true;
}
else
{
returnObjectLine=false;
}
}
在我的 robot.cpp 中,我有这段代码,它获取两个布尔值,然后输出到控制台:
void robot::getReturnObject()
{
if(returnObjectDash==true)
{
std::cout<<"Dash\n";
//dir=DOWN;
}
if(returnObjectLine==true)
{
std::cout<<"Line\n";
//dir=DOWN;
}
}
这是我的 main.cpp
int main()
{
robot r;
while(true)
{
matrix m;
m.robotPosition(r.getX(), r.getY());
m.update(); // vector2D init and draw
m.detection(m.getX(), m.getY());
r.update();
Sleep(250);
}
}
我将我的 matrix.cpp 默认构造函数中的两个布尔变量的默认值设置为 false。当我点击暂停按钮并进行调试时,我似乎得到了两种不同的回报。对于我的矩阵,它返回假,但对于我的机器人,它返回真,就像我的程序正在制作两个不同的变量。如果有人可以对这种奇怪的行为有所了解,那么请告诉!谢谢
【问题讨论】:
-
"默认值为 true" -- 不,默认值未定义。
-
在机器人上设置 returnObject* 变量的代码在哪里?矩阵码在哪里?您希望结果匹配的调用代码是什么?
-
哦,对不起,我会更新我的问题。
-
if(returnObjectDash==true)最好写成if(returnObjectDash)