SFML community forums

Help => General => Topic started by: Tojmak on December 16, 2019, 09:30:57 pm

Title: STL List object with sprite
Post by: Tojmak on December 16, 2019, 09:30:57 pm
Hello, when i run game which i'm making i get error : "Cannot dereference end list iterator". I search for something that could help me but without any breakthroughs. So maybe you can help me. So... i have class - Chests, which objects stores sprite. objects of that class are stored in STL List container. Error which i refered to before appears after that line of code:

for (int i = 0; i < chestAmount; i++)
        {
                advance(chestIterator, i);     
                chestIterator->DrawChests();
        }
 

chestIterator is type -> list<Chests>iterator

Inside DrawChests() method i have just:

void Chests::DrawChests()
   {
           TChests.loadFromFile("images/chest.png");
           SChests.setTexture(TChests);
   }

 


And yeah... i think thats it. Propably something stupid (i wish) since thats my first time using list container (before that was vector but i;ve had problem with reallocating memory. Thanks for any ideas.
Title: Re: STL List object with sprite
Post by: Nexus on December 16, 2019, 09:34:15 pm
If you have an iterator, why don't you use it as a loop variable?
for (std::list<Chests>::iterator itr = chests.begin(); itr != chests.end(); ++itr)
   itr->DrawChests();
 

Or better:
for (auto itr = chests.begin(); itr != chests.end(); ++itr)
   itr->DrawChests();
 

And even better:
for (Chests& c : chests)
  c.DrawChests();
 

By the way, this has nothing to do with SFML, but is basic C++. You might want to read up some parts in a modern C++ book before delving into game development ;)
Title: Re: STL List object with sprite
Post by: Tojmak on December 17, 2019, 09:18:57 am
Yeah.. i'm okay with that. I'm not saying that i don't know your version of facing that problem, but i'm rather wondering what's the difference? From what i see only thing that your version is different from mine is that yours miss advance method, of course beside optimalisation things. So how that should that solve my problem? And yeah, clearly i don;t know list very well, as i said before it used to be vector, but problem rose up before me and i find that people suggest to use list.
Title: Re: STL List object with sprite
Post by: Rosme on December 17, 2019, 03:18:36 pm
I'd be really curious to see why a list is better than a vector in your case (in most case, a list is not better than a vector). Also, the difference between his and yours is that he check if the iterator has reached end(). You don't which is exactly what your error is saying. Cannot deference end list iterator.
Title: Re: STL List object with sprite
Post by: Tojmak on December 18, 2019, 04:13:34 pm
Not sure if understand, i mean... i have condition that i<chestAmount, so it's not like i go outside of list. And for choosing list, i had white box problem, because after one of boxes where open erase method was called which causes memory relocation. From what i read in moments like that it's better to use list or vector with inteligent pointer, or am i wrong in your opinion?
Title: Re: STL List object with sprite
Post by: Rosme on December 18, 2019, 06:28:17 pm
You need to make sure you scope your memory correctly. A resource manager is what you probably need. This is not a good case for a list.

As for the check with chestAmount, there's nothing in the code here that indicates that the size of your list is equal to the chestAmount. The runtime error says it. You are trying dereference the end iterator. So you are going too far.
Title: Re: STL List object with sprite
Post by: Laurent on December 19, 2019, 08:21:36 am
for (int i = 0; i < chestAmount; i++)
{
    advance(chestIterator, i);
    ... use chestIterator ...
}

Iteration #1: chestIterator advances by 0, it points to element #0 (given that it is initialized to begin())
Iteration #2: chestIterator advances by 1, it points to element #1
Iteration #3: chestIterator advances by 2, it points to element #3
Iteration #4: chestIterator advances by 3, it points to element #6
... I hope you see the problem ;)

But more importantly, this is not a decent way to iterate over a collection, so although it's good to understand why it failed, don't try to fix it -- see Nexus post for real solutions!
Title: Re: STL List object with sprite
Post by: Tojmak on December 21, 2019, 02:24:02 pm
Thanks for all your help. Now i think everything is clear :)