Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 是什么导致布尔输出一个随机数?_C++_Boolean_Undefined Behavior - Fatal编程技术网

C++ 是什么导致布尔输出一个随机数?

C++ 是什么导致布尔输出一个随机数?,c++,boolean,undefined-behavior,C++,Boolean,Undefined Behavior,我正在开发我的游戏,在实现中,我有来自项目不同位置的代码块。我正在检查变量isCreated是否为false,以将其绘制到GUI,但相反,cosnole会在100秒内的某个位置输出一个随机数(最近的尝试显示104和120)。代码中是否存在导致地址问题的内容?我不知道当布尔值被声明时,这样的事情怎么会发生 注意:inputmenuuptr是指向inputmenuugui的智能指针 编辑:isCreated输出为0,直到我进入一致性测试#3 另一个编辑:下面是证明setter函数实际上正在使用的代码

我正在开发我的游戏,在实现中,我有来自项目不同位置的代码块。我正在检查变量
isCreated
是否为false,以将其绘制到GUI,但相反,cosnole会在100秒内的某个位置输出一个随机数(最近的尝试显示104和120)。代码中是否存在导致地址问题的内容?我不知道当布尔值被声明时,这样的事情怎么会发生

注意:
inputmenuuptr
是指向
inputmenuugui
的智能指针

编辑:
isCreated
输出为0,直到我进入一致性测试#3

另一个编辑:下面是证明setter函数实际上正在使用的代码

InputMenuGUI testMenuGUI("custom"); //create object

InputMenuGUI(std::string s){setter(s);}; // calls this constructor

void setter(std::string s){ myStyle=s;
            isCreated=false;isClean=false;isActive=false;}

//this is the setter function.
从原来的

struct InputMenuGUI{
        InputMenu *myMenu;
        ScrImage iMenu, tMenu;
        Sprite sMenu[3];
        Paragraph pMenu;
        Coord2 pos;///Pos is Top Mid of Menu Border
        bool isCreated, isClean, isActive;
        std::string myStyle;
        Button clickRange;
        std::map<int, ScrImage> screenMap;
        std::map<int, Sprite> spriteMap;
        std::map<int, Paragraph> textMap;
        std::map<int, Button> buttonMap;
        ///--------------------------------------------
        InputMenuGUI(){setter("custom");};
        InputMenuGUI(std::string s){setter(s);};
        void setter(std::string s){ myStyle=s;
            isCreated=false;isClean=false;isActive=false;}
        void display(Gorilla::Silverback *gameSB, Ogre::Viewport *gameVP, Coord2 anchorT);///->

        }

void GameApp::startInputMenu(){
        {///Escape Menu (Testing Phase)
            //Create menu, add params
            InputMenu testMenu("Really Exit?");
            testMenu.addButton(ChoiceButton("Please!",1,10));
            testMenu.addButton(ChoiceButton("I'm kiddin!",2,12));
            //create menu's gui, link ptr of menu, set menu gui ptr in map
            InputMenuGUI testMenuGUI("custom");
            std::cout<<"testing: " << testMenuGUI.isCreated << std::endl;
            testMenuGUI.myMenu=&testMenu;
            std::cout<<"testing2: " << testMenuGUI.isCreated << std::endl;
            appMenu["ExitMenu"].reset(&testMenuGUI);
            printf("Testing Consistency #%d: %d\n", 1, testMenuGUI.isCreated);
            printf("Testing Consistency #%d: %d\n", 2, appMenu["ExitMenu"]->isCreated);
        }
        ///start the input menus for GUI
    }

void GameApp::loopInput(){
    if(myKeyboard->isKeyDown(OIS::KC_ESCAPE)){
                std::cout<< "Pushing menu"<<std::endl;
                printf("Testing Consistency #%d: %d\n", 5, appMenu["ExitMenu"]->isCreated);
                myGUI.pushMenu(appMenu["ExitMenu"]);
            }
{

void GameUI::pushMenu(InputMenuPtr i){
        Coord2 tAnchor((*myGameAnchor)["T"]);
        tAnchor.add(0,(*myGameAnchor)["B"].y/4);
        printf("Testing Consistency #%d: %d\n", 4, i->isCreated);
        ///Note: when changing res, update menu positions
        ///ToDo: Movable menus. if you hold the mouse button down, have notes on the last pos to make a relative movement
        activeMenus.push_back(i);
        printf("Testing Consistency #%d: %d\n", 3, i->isCreated);
        i->display(myGameSB,myGameVP,tAnchor);
        std::cout<<"Menu pushed"<<std::endl;
    }

void InputMenuGUI::display(Gorilla::Silverback *gameSB, Ogre::Viewport *gameVP, Coord2 anchorT){
        std::cout<<"testing created: "<<isCreated<<std::endl;
        if(!isCreated){

        //...
        }
struct InputMenuGUI{
InputMenu*myMenu;
ScrImage iMenu,tMenu;
Sprite sMenu[3];
第pMenu段;
Coord2 pos;///pos位于菜单边框的中间上方
布尔是创造的,是清洁的,是活跃的;
std::字符串myStyle;
按钮点击范围;
std::地图屏幕图;
地图精灵地图;
std::map textMap;
标准::映射按钮映射;
///--------------------------------------------
InputMenuGUI(){setter(“custom”);};
InputMenuGUI(std::string s){setter(s);};
void setter(std::string s){myStyle=s;
isCreated=false;isClean=false;isActive=false;}
虚空显示(大猩猩::银背*游戏SB,食人魔::视口*游戏VP,Coord2锚地);//->
}
void GameApp::startInputMenu(){
{///转义菜单(测试阶段)
//创建菜单,添加参数
InputMenu testMenu(“是否真的退出?”);
testMenu.addButton(ChoiceButton(“Please!”,1,10));
addButton(ChoiceButton(“我是kiddin!”,2,12));
//创建菜单的gui,链接菜单的ptr,在地图中设置菜单gui ptr
InputMenuGUI testMenuGUI(“自定义”);

std::cout
是创建的
肯定是未初始化的。在这种情况下,它的值通常是其内存位置上的任何值。

被提升为良好猜测。同时,我希望有一种机制也可以对回答不真正应该回答的问题进行否决。因为越来越多的海报了解到,他们可以要求猜测b只要抛出一些随机信息,噪声与信号的比率就增加得越多。当我构造对象时,它立即进入
setter()
variable,这使得
isCreated=false;
我一直在这样做,这里怎么不起作用?@Molma在你的例子中,你在打印成员之前没有调用setter。而且,初始化应该总是发生。编写正确的构造函数!我编辑了文章,以显示证明setter函数正确的行g使用。投票关闭作为缺少示例。未显示相关代码。调试器将非常有用。简短回答:内存损坏/代码中未显示的未定义行为。这是所有与对象相关的代码,除了InputMenuGUI的整个源文件外,没有其他要添加的内容(长度超过100行)