【问题标题】:Weird behaviour with boolean value布尔值的奇怪行为
【发布时间】: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)

标签: c++ boolean simulator


【解决方案1】:

您的程序正在生成两个不同的值,因为它具有两个不同的值。 matrix 类显然有自己的布尔变量matrix::returnObjectDash,机器人类有自己的变量robot::returnObjectDash。在一个类的一个实例中设置变量不会影响任何其他类(或任何其他实例)中的变量。

【讨论】:

  • 好的,考虑到这个逻辑,我必须在我的 main.cpp 中设置 returnObject,他们都可以访问它?
  • 您可以使用他们都可以访问的单个变量,是的。为了实现这一点,在技术层面上,您将向机器人实例和矩阵实例传递指向同一变量的指针或引用。我对设计没有足够的了解,不知道这是否是一个好的计划。
  • 我实际上是在考虑把它变成一个指针然后引用它,谢谢你也提出了这个想法。谢谢。
【解决方案2】:

您没有向矩阵类提供代码,但是,从void matrix::detection(int x, int y) 来看,我假设您在其中有一个称为检测的方法,并且您已声明相同的字段returnObjectLinereturnObjectDash

假设这些变量有 2 个版本是正确的:其中一个版本在您的矩阵对象内,另一个在您的机器人对象内。

不仅如此,您还可以(而且通常会这样做!)拥有多个矩阵/机器人对象。这些变量中的每一个都有自己单独的这些变量副本,更改其中一个不会影响其他变量。

【讨论】:

  • 啊,是的,关于拥有多个矩阵或机器人的好处。请参阅原始问题以获取更新。
猜你喜欢
  • 1970-01-01
  • 2021-12-31
  • 2012-12-01
  • 2014-06-20
  • 1970-01-01
  • 2012-06-21
  • 1970-01-01
  • 1970-01-01
  • 2014-09-01
相关资源
最近更新 更多