Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: STL List object with sprite  (Read 4166 times)

0 Members and 1 Guest are viewing this topic.

Tojmak

  • Newbie
  • *
  • Posts: 4
    • View Profile
STL List object with sprite
« 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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: STL List object with sprite
« Reply #1 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 ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Tojmak

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: STL List object with sprite
« Reply #2 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.

Rosme

  • Full Member
  • ***
  • Posts: 169
  • Proud member of the shoe club
    • View Profile
    • Code-Concept
Re: STL List object with sprite
« Reply #3 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.
GitHub
Code Concept
Twitter
Rosme on IRC/Discord

Tojmak

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: STL List object with sprite
« Reply #4 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?
« Last Edit: December 18, 2019, 04:17:14 pm by Tojmak »

Rosme

  • Full Member
  • ***
  • Posts: 169
  • Proud member of the shoe club
    • View Profile
    • Code-Concept
Re: STL List object with sprite
« Reply #5 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.
GitHub
Code Concept
Twitter
Rosme on IRC/Discord

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: STL List object with sprite
« Reply #6 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!
Laurent Gomila - SFML developer

Tojmak

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: STL List object with sprite
« Reply #7 on: December 21, 2019, 02:24:02 pm »
Thanks for all your help. Now i think everything is clear :)