2016-02-29 26 views
-1

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

注意:InputMenuPtr是一个智能指针InputMenuGUI

编辑:isCreated输出作为0直到我得到一致性测试#3 ...

ANOTHER编辑:下面是证明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){ 

     //... 
     } 
+0

投票结束,因为缺乏示例。相关代码未显示。 –

+0

调试器将是最有帮助的。 –

+0

简短回答:内存损坏/未定义的行为在代码中没有显示在这里。 –

回答

1

isCreated肯定是未初始化。在那种情况下,它的价值通常是目前在其存储位置处的任何值。

+0

Upvoted as good guess。同时,我希望有一种机制能够*还* *回答那些不真正应付的问题。因为越多海报知道他们可以通过丢弃一些随机信息来征求猜测,噪声与信号的比率就越高。 –

+0

当我构造对象时,它立即进入'setter()'变量,这使得'isCreated = false;'我一直这样做,它在这里怎么不起作用? – Molma

+2

@Molma在你的例子中,你不打电话给你的setter打印成员之前。此外,应始终进行初始化。编写适当的构造函数! –

相关问题