2016-02-29 65 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打印成員之前。此外,應始終進行初始化。編寫適當的構造函數! –

相關問題