2

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.

  • 1
    Are you following a guide that suggested using `std::iterator` like this? – Stephen Newell Aug 09 '20 at 18:09
  • 1
    C++ is complicated, you should read some learning material (preferably a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)). Just having ideas and then getting confused when they don't work is not going to get you very far. A book will teach you the principles behind the language. – john Aug 09 '20 at 18:31
  • I agree C++ is complicated, but i am trying to learn with what i can get online. Getting a book however, is what i just might do in near future. @john – logoslavikus Aug 10 '20 at 11:16
  • All i found on encppreference, if that is what you mean by "guide", is that i can declare it like this ```std::iterator iteratorName``` Any example code is declared differently not as i want it. I know for example that with strings there is `std::string::iterator` and that makes thing easier *EDIT* as i see in the other answers, something similar to that is the case with what i am trying to accomplish too... – logoslavikus Aug 10 '20 at 11:18
  • @logoslavikus I'd like to know on cppreference where it said you can declare `std::iterator iteratorName` because that is not correct. cppreference is normally very reliable (although not really aimed at beginners). – john Aug 10 '20 at 11:58
  • I just checked my history trough out whole day yesterday and i made a mistake. It seems i permuted something and i couldn't find any source from where i read it which basically means i might as well made it up. Sincerely sorry, i should think twice before making these blunt statements . – logoslavikus Aug 10 '20 at 12:07

2 Answers2

2

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&).

JFMR
  • 23,265
  • 4
  • 52
  • 76
1

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.

Rohan Bari
  • 7,482
  • 3
  • 14
  • 34