I have a city node which houses many building nodes, each of these I wish to grant a new child-node. which tells the house what role and sign they have/role. Which can later be used for other functions. for now all get the same .mesh (will later make a sign) to identify which house is what. it shall be randomly assigned. If I try to run this I get the following error. I am very new to ogre , which adds to some weird code
int CityManager::assignBuildingRole(Ogre::SceneNode * _cityNode, int _numberOfBuildings)
{
std::stringstream nodename("buildingRoleNode");
Ogre::SceneNode::ChildNodeIterator cNode = _cityNode->getChildIterator();
std::vector <Ogre::SceneNode*> detachable;
while (cNode.hasMoreElements()) {
detachable.push_back((Ogre::SceneNode *)cNode.getNext());
}
for (int i = 0; i < detachable.size(); i++) {
nodename << childIteration << "_" << parentIteration << "_" << i;
switch (rand() % 5) // assign building random proffessions by giving them a rolenode
{
case 0:
_roleNode = ( Ogre::SceneNode *)cNode.getNext()->createChild(nodename.str());
_signEntity = manager->createEntity("Barrel.mesh");
_roleNode->attachObject(_signEntity);
break;
case 1:
_roleNode = (Ogre::SceneNode *)cNode.getNext()->createChild(nodename.str());
_signEntity = manager->createEntity("Barrel.mesh");
_roleNode->attachObject(_signEntity);
break;
case 2:
_roleNode = (Ogre::SceneNode *)cNode.getNext()->createChild(nodename.str());
_signEntity = manager->createEntity("Barrel.mesh");
_roleNode->attachObject(_signEntity);
break;
case 3:
_roleNode = (Ogre::SceneNode *)cNode.getNext()->createChild(nodename.str());
_signEntity = manager->createEntity("Barrel.mesh");
_roleNode->attachObject(_signEntity);
break;
case 4:
_roleNode = (Ogre::SceneNode *)cNode.getNext()->createChild(nodename.str());
_signEntity = manager->createEntity("Barrel.mesh");
_roleNode->attachObject(_signEntity);
break;
default:
break;
}
}
return role;
}

Calling
getNextonChildNodeIteratorwill not only retrieve the next element but advance to the next element, too. See its description in documentationSo what you do is iterating over the children of
_cityNodeand store every child in yourdetachablevector.After these lines your
cNodepoints to end of children list (specifically after the last element, asend()instd::mapwhich is the underlying data type)If you are trying to iterate it further you'll get an error.
You can either use
detachablefor the next iteration (I guess that's why you created it)or do the child creation in your while loop and don't use
detachableat all.