2017-04-06 59 views
2

我正在爲我的pcg遊戲進行城市生成。我有一個循環,這使得3個城市的隨機位置,我給你parentIteration得到那個「ID」爲城市做同樣的for圈,我做出一個建築Ogre3d具有獨特的節點名稱錯誤

for (int i = 0; i < 3; i++) 
{ 
    parentIteration = i; 
    std::srand(i); 
    _rootNode = GameManager::getSingletonPtr()->getSceneManager()->getRootSceneNode(); 

    _cityNode = _rootNode->createChildSceneNode("cityNode " + parentIteration); 
    generateCity(std::rand() % 10000 + 10, std::rand() % 10000 + 10, std::rand() % 11 +1); 
} 

建設

for (int i = 0; i < _numberOfBuildings; i++) 
    { 
     childIteration = i; 
     printf(" parent %d and child %d \n", parentIteration, childIteration); 
     Ogre::SceneNode* buildingNode = _cityNode->createChildSceneNode("citybuildingNode"+childIteration+parentIteration); 
} 

但是,當我嘗試啓動遊戲時,它會在創建第二個城市時崩潰。說它已經有一個類似於它試圖寫的名字。然而我的printf清楚地表明,那時的數字都是獨一無二的。任何人都知道如何解決這個問題? (輸出證明添加圖片)

enter image description here

+0

你確定''citybuildingNode「+ childIteration + parentIteration'是否符合你的想法?因爲我認爲你打算做一個字符串連接而不是指針算術。 'std :: string(「citybuildingNode」)+ childIteration + parentIteration'似乎更合適 – PeterT

+0

'cityNode = _rootNode-> createChildSceneNode(「cityNode」+ parentIteration);' - 我不知道'Ogre',重新傳球 - 預期的實際類型是什麼?你正在添加一個字符串文字和一個「int」。這不會像預期的那樣工作,除非有一些我不知道的魔法。 – PaulMcKenzie

+0

[可能的重複](http://stackoverflow.com/questions/191757/how-to-concatenate-a-stdstring-and-an-int)。這可能是你的問題歸結爲什麼。 C++不像其他語言那樣使用字符串文字。你不能只是將一個數字連接到一個文字上,並得到一個你期望的字符串。 – PaulMcKenzie

回答

2

錯誤消息的「itybuildingNode」,這表明

"citybuildingNode"+childIteration+parentIteration 

工作不挺你想要的方式。

這是因爲對你的工作有兩件事情:

  1. 「citybuildingNode」是一個字符串,而不是字符串對象。它只是一排由空字符結尾的字符,表示爲const char *,指向該字符數組的指針。它是低級伏都教,這種東西你可能會做一個字符串類。 For more information see String Literals

  2. ,因爲它不是一個字符串對象,你可以不拉任何常見字符串對象的技巧,比如用+串聯,並與==比較。但是因爲它是一個指針,所以編譯器將+解釋爲嘗試執行指針算術並引用數組中的另一個位置。它編譯,但注意它是如何將「citybuildingNode」變成「itybuildingNode」的。哎呀。

什麼,這看起來像是一樣的東西:

const char* temp = "citybuildingNode" 
_cityNode->createChildSceneNode(temp + childIteration + parentIteration); 

解析爲

const char* temp = "citybuildingNode" 
_cityNode->createChildSceneNode(&temp[childIteration + parentIteration]); 
  • 即使它是一個字符串對象,該C++標準字符串對象std::string不允許您向字符串添加數字。它只將字符串添加到一起以構建更大的字符串。要將號碼添加到std::string,您必須將號碼轉換爲std::stringstd::to_string可以幫助你在這裏,但有一個更清潔的前瞻性方式與std::stringstream
  • 例如,要做到這一點:

    std::stringstream nodename("citybuildingNode"); 
    // builds a string stream around the string literal 
    nodename << childIteration << parentIteration; 
    // writes the numbers into the stream the same way `cin << number;` would 
    // turning the number into a string for you 
    Ogre::SceneNode* buildingNode = _cityNode->createChildSceneNode(nodename.str()); 
    // gets the assembled string from the stringstream 
    // null-terminated string like ogre expects 
    

    這讓你在正確的方向開始,但仍允許孩子之間的碰撞1和父母10(「citybuildingNode110」)以及孩子11和父母0(也是「citybuildingNode110」)等。所以你真的想要更像

    nodename << childIteration << '_' << parentIteration; 
    

    強制兩個數字之間的分隔符。

    Documentation for std::stringstream.

    也有另一種可能的討厭。我們剛剛提供給食人魔的string只會在std::stringstream nodename存在的情況下存在,並且會在產生它的循環結束時死掉。我沒有看到任何東西the documentation快速細讀,說食人魔做它自己的副本string。所以請仔細研究一下,以確保你不必在某個地方存儲這個名字,以防止它掉到範圍之外,被摧毀,離開食人魔dangling reference

    +0

    我想感謝您對我的錯誤以及爲什麼我犯了錯誤以及如何解決錯誤的明確解釋! – KevinTheGreat

    相關問題