2017-04-06 56 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

    相关问题