저는 대학 프로젝트 용 로봇 시뮬레이터를 설계하고 있습니다. 충돌 감지에 큰 문제가 있습니다.부울 값이있는 이상한 동작
#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
여기 내 robot.h 헤더 파일입니다. 내 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;
}
}
내가 콘솔에 두 개의 부울 값을 가져온 다음 출력이 코드를 가지고 :
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를 반환하지만, 내 로봇은 사실을 반환하지만 내 프로그램은 두 개의 다른 변수를 만드는 것과 같습니다. 누군가가이 이상한 행동에 대해 밝힐 수 있다면 알려주세요. 매트릭스 클래스, 그러나, void matrix::detection(int x, int y)
에서 판단 나는 당신이 감지라는 존재의 방법을 가지고 있다고 가정하고 같은 필드 returnObjectLine
및 returnObjectDash
를 선언 한 것을 당신은 당신에게 코드를 제공하지 않은 당신에게
"기본값은 true입니다."- 아니요, 기본값은 정의되지 않습니다. – addaon
로봇에서 returnObject * 변수를 설정하는 코드는 어디에 있습니까? 매트릭스 코드는 어디에 있습니까? 결과가 일치 할 것으로 예상하는 호출 코드는 무엇입니까? – addaon
오, 미안 해요, 제 질문을 업데이트 할게요. –