Domanda

I'm designing a robot simulator for a university project and I've hit a big issue for some collision detection. Here is my robot.h header file:

#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

Basically, I have two boolean values being used: returnObjectDash;and returnObjectLine. I have this code in my matrix.cpp file:

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;
    }
}

Inside my robot.cpp I have this code which gets the two boolean values and then outputs to the console:

void robot::getReturnObject()
{
    if(returnObjectDash==true)
    {
        std::cout<<"Dash\n";
        //dir=DOWN;
    }
    if(returnObjectLine==true)
    {
        std::cout<<"Line\n";
        //dir=DOWN;
    }
}

This is my 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);
    }
}

I'm setting the default value of my two boolean variables in my matrix.cpp default constructor to false. When I hit the pause button and debug I seem to be getting two different returns. For my matrix it is returning false, though for my robot it is returning true, it is like my program is making two different variables. If someone could shed some light on this weird behaviour, then please do tell! Thank you

È stato utile?

Soluzione

Your program is making two different values because it has two different values. The matrix class apparently has its own Boolean variable, matrix::returnObjectDash, and the robot class has its own variable, robot::returnObjectDash. Setting the variable in one instance of one class has no impact on the variables in any other classes (or any other instances).

Altri suggerimenti

You have not provided code to you matrix class, however, judging from void matrix::detection(int x, int y) I assume that you have a method in there, called detection, and that you have declared the same fields returnObjectLine and returnObjectDash.

You are correct in assuming that there are 2 versions of those variables: One of those versions is inside your matrix object, and the other one is inside your robot object.

Not only that, you can (and usually do!) have more than one matrix/robot object. Each of those will have its own separate copy of those variables, and changing one of them will not impact the other ones.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top