This code won't compile:
for(vector<Box>::iterator it = shapes.end(); it >= shapes.begin(); --it){
*it.update(1,1);
*it.draw();
}
It claims:
main.cpp:80:17: error: ‘std::vector<Box>::iterator’ has no member named ‘update’
main.cpp:81:17: error: ‘std::vector<Box>::iterator’ has no member named ‘draw’
However AFAIK, that code doesn't try and call vector::iterator.draw(), it dereferences the iterator, which should give me an object of my class box, which does have those methods. What am I doing wrong, and sorry for the awful title.
It's a matter of operator precedence.
Operator . has higher precedence than operator *. Use parenthesis to force operator * application first.
(*it).update(1,1);
(*it).draw();
You can also use operator -> on iterators.
it->update(1,1);
it->draw();
Also see: What is the difference between the dot (.) operator and -> in C++? and cppreference: Member access operators.
for(vector<Box>::reverse_iterator it = shapes.rbegin(); it != shapes.rend(); ++it)
{
it->update(1,1);
it->draw();
}
also, to add on @Pixelchemist 's answer.
for(vector<Box>::iterator it = shapes.end(); it >= shapes.begin(); --it){
*it.update(1,1);
*it.draw();
}
should be:
for(vector<Box>::reverse_iterator it = shapes.rbegin(); it != shapes.rend(); ++it){
*it.update(1,1);
*it.draw();
}
rend and rbeing are used to iterate in reverse.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With