argument list for class template "std::iterator" is missing

Viewed 561

argument list for class template "std::iterator" is missing

I get this error with this occasion:

for(std::iterator it = deque.begin(); it != deque.end(); it++)

So i had an idea of specifying in template manner like this:

for(std::iterator<sf::RectangleShape> it = deque.begin(); it != deque.end(); it++)

But then i get different error:

expected a declaration

I am kind of confusing on what is going here. All i need is the iterator because that way i can get to the last element of std::deque and set it to a different rectangle color.

Thanks in advance.

2 Answers

Assuming you want to iterate through a std::deque<sf::RectangleShape>: The type returned by the non-const member functions begin() and end() of a std::deque<sf::RectangleShape> container is neither std::iterator nor std::iterator<sf::RectangleShape> but actually std::deque<sf::RectangleShape>::iterator:

for (std::deque<sf::RectangleShape>::iterator it = deque.begin(); it != deque.end(); ++it)

As already suggested in this other answer, you can, since C++11, simply rely on auto:

for (auto it = deque.begin(); it != deque.end(); ++it)

Having to write just auto instead of std::deque<sf::RectangleShape>::iterator is probably less prone to errors, as you may have already noticed. Even better, as you are iterating over the whole container, you can, since C++11, just use a range-for loop instead:

for (auto& elem: deque)

In this case, elem is not an iterator to an element of deque but a reference to the element itself (i.e., sf::RectangleShape&).

Try to declare the iterator with auto:

for (auto it = deque.begin(); it != deque.end(); it++)

to let the compiler decide the appropriate type for it.

Related