我正在爲一個大學項目設計一個機器人模擬器,並且我碰到了一個碰撞檢測的大問題。這是我的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。當我點擊暫停按鈕並調試時,我似乎得到了兩個不同的回報。對於我的矩陣,它返回false,但對於我的機器人來說,它返回true,這就像我的程序正在創建兩個不同的變量。如果有人能夠揭示這種奇怪的行爲,那麼請告訴我!謝謝
「默認值爲真」 - 不,默認值是未定義的。 – addaon
在機器人上設置returnObject *變量的代碼在哪裏?矩陣碼在哪裏?什麼是您的調用代碼,以便您期望結果匹配? – addaon
對不起,我會更新我的問題。 –