Iterating through a list

I have a list of blocks that I have created, and I need to iterate through them and draw each one. here's what I have, but I keep getting an error : conversion from 'std::cxx11::list<Block>iterator {aka std::_List_iterator<Block>}' to non-scalar type 'Block' requested Block block = blockIt
I see where the problem is, but I'm not sure how to fix it. The last time I had to iterate through a list, it was a list of pointers and this time it isn't, so I'm not sure what to do differently.


1
2
3
  for (list<Block>::iterator blockIt = blocks.begin(); blockIt != blocks.end(); blockIt++)
    {Block block = blockIt;
     block.draw();}
Perhaps

Block block = *blockIt;

Why bother with an assignment?

blockIt->draw();

that did it, thanks!
This sort of problem is why we now have range based for loops:
1
2
3
for (const Block &block : blocks) {
    block.draw();
}
Topic archived. No new replies allowed.