-
I'm having trouble with managing entities; I'm always getting "vector iterator not incrementable ".
(below is some pseudo code)
class Game;
class Entity {
public:
void update(sf::Time dt, Game& game) {
game.addEntity(); //ERROR IS HERE
}
};
class Game {
public:
Game() : mEntities() {
addEntity();
}
void addEntity(Entity* e) {
mEntities.push_back(e);
}
vector<Entity*> mEntities;
};
When I try calling 'addEntity()' from a different class I get an error. :-\
-
It's more an C++ issue than a SFML one...
But
void addEntity(Entity* e)
is waiting for an Entity*, but you do not pass any in here
game.addEntity();
-
Well I fixed that issue, and now I'm still getting the same error.
-
Can I see what you did, I am pretty sure you did not fix it properly.
-
Well in my Game.cpp I have methods for updating, rendering, etc.
void Game::update(sf::Time dt) {
for (auto& it : mEntities)
it->update(dt);
}
void Game::render() {
for (const auto& it : mEntities)
mWindow.draw(*it);
}
void Game::addEntity(Entity* e) {
mEntities.push_back(e);
}
-
Well compiler errors are a wonderful thing since they point you directly to the related code, unlike your post that just says that there's an error *somewhere*. ;)
-
Well in my Game.cpp I have methods for updating, rendering, etc.
void Game::update(sf::Time dt) {
for (auto& it : mEntities)
it->update(dt);
}
void Game::render() {
for (const auto& it : mEntities)
mWindow.draw(*it);
}
void Game::addEntity(Entity* e) {
mEntities.push_back(e);
}
Not sure if I'm right or not, but shouldn't your ranged based loops have the following format instead of how you did it? :
for ( Entity* e : mEntities )
{
/* do stuff */
}
Or is that the same thing as what you already have?
-
It's the "same". The auto keyword gets the correct type for you upon compilation.
-
It's the "same". The auto keyword gets the correct type for you upon compilation.
Oh thats right ::) I was thinking of something else, but that makes sense
-
Do you think it's because I add another entity in another entity's 'update' function, and the vector is getting invalidated?
-
Well, this forum is actually for SFML-related problems. We occasionally answer other questions related to game development, but C++ language-specific questions shouldn't be the standard, there are better places for them (e.g. C++ forums, StackOverflow).
But if you ask here, you have to adhere to the forum rules, in particular you should post a minimal complete example that reproduces the problem (http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368). You should also try with the debugger to find out as much as possible. This reduces the amount of guesswork on our side, and you will get a meaningful answer much faster.