找到你要的答案

Q:Enums from another class in C++ (with SFML)

Q:在C++从另一班的Enums(与SFML)

I'm using this code:

void GameObjectManager::DrawHUD(sf::RenderWindow& renderWindow)
{
    std::map<std::string, VisibleGameObject*>::const_iterator itr = _gameObjects.begin();
    while (itr != _gameObjects.end())
    {
        //if (itr->second->GetObjectType() == VisibleGameObject::HUD)
        //{
            itr->second->Draw(renderWindow);
            itr++;
        //}
    }
}

To draw all elements of ObjectType HUD. ObjectType is an enum in my other class VisibleGameObject. If I use the slashes in my code (so when I don't check for the enum) everything works fine. But when I only want to draw objects of ObjectType HUD my program just gets stuck in Debug mode. I don't get any errors in debug or anything. It's just stuck on drawing the entire thing.

Does anybody see what I'm doing wrong?

Thanks in advance,

Xentro

我使用这个代码:

void GameObjectManager::DrawHUD(sf::RenderWindow& renderWindow)
{
    std::map<std::string, VisibleGameObject*>::const_iterator itr = _gameObjects.begin();
    while (itr != _gameObjects.end())
    {
        //if (itr->second->GetObjectType() == VisibleGameObject::HUD)
        //{
            itr->second->Draw(renderWindow);
            itr++;
        //}
    }
}

绘制对象类型的HUD元素。ObjectType是我的另visiblegameobject枚举类。如果我在我的代码中使用斜线(所以当我不检查枚举)一切都好。但当我只想画我的程序对象对象类型HUD只会停留在调试模式。我没有任何错误的调试或任何。它只是停留在画整个事情。

有人看到我做错了什么吗?

先谢谢了。

xentro

answer1: 回答1:

The problem you are having is if the condition is not true then you never increment the iterator. If you never increment the iterator it will never be equal to end(). You need to always increment the iterator and in your if statement do your processing:

void GameObjectManager::DrawHUD(sf::RenderWindow& renderWindow)
{
    std::map<std::string, VisibleGameObject*>::const_iterator itr = _gameObjects.begin();
    while (itr != _gameObjects.end())
    {
        if (itr->second->GetObjectType() == VisibleGameObject::HUD)
        {
            itr->second->Draw(renderWindow);
        }
        itr++;  // unconditional increment.
    }
}

If you want you can use ranged based for loops from C++11 and up like:

void GameObjectManager::DrawHUD(sf::RenderWindow& renderWindow)
{
    for (auto & e : _gameObjects)
    {
        if (e.second->GetObjectType() == VisibleGameObject::HUD)
            e.second->Draw(renderWindow);
    }
}

你的问题是,如果条件不正确,那么你永远不会增加迭代器。如果你没有增量的迭代器不等于()。您需要始终增加迭代器,并在if语句中进行处理:

void GameObjectManager::DrawHUD(sf::RenderWindow& renderWindow)
{
    std::map<std::string, VisibleGameObject*>::const_iterator itr = _gameObjects.begin();
    while (itr != _gameObjects.end())
    {
        if (itr->second->GetObjectType() == VisibleGameObject::HUD)
        {
            itr->second->Draw(renderWindow);
        }
        itr++;  // unconditional increment.
    }
}

如果你想你可以使用的范围为基础的C++ 11和像环:

void GameObjectManager::DrawHUD(sf::RenderWindow& renderWindow)
{
    for (auto & e : _gameObjects)
    {
        if (e.second->GetObjectType() == VisibleGameObject::HUD)
            e.second->Draw(renderWindow);
    }
}
c++  enums