Hi everyone, I am trying to clone space invaders with SDL and have created a bullet class and a vector to store them. I am trying to implement the function that checks if they're off the screen:
when I try to compile it gives me this error:Code:void Player::handleBullets(SDL_Surface *screen, Uint32 deltaTicks){ int i; std::vector<Bullet>::reverse_iterator rii; for(rii=bullets.begin(); rii!=bullets.end(); ++rii){ if(!*rii.move(deltaTicks)){ // Player::move() returns false if the bullet is off the screen bullets.erase(rii); }else{ *rri.show(screen); } } }
BTW: the code was taken from a tutorial on vectors and modified by me, also all the functions that are called existCode:../src/player.cpp: In member function ‘void Player::handleBullets(SDL_Surface*, Uint32)’: ../src/player.cpp:56: error: no match for ‘operator=’ in ‘rii = ((Player*)this)->Player::bullets.std::vector<_Tp, _Alloc>::begin [with _Tp = Bullet, _Alloc = std::allocator<Bullet>]()’ /usr/lib/gcc/i686-pc-linux-gnu/4.4.3/../../../../include/c++/4.4.3/bits/stl_iterator.h:96: note: candidates are: std::reverse_iterator<__gnu_cxx::__normal_iterator<Bullet*, std::vector<Bullet, std::allocator<Bullet> > > >& std::reverse_iterator<__gnu_cxx::__normal_iterator<Bullet*, std::vector<Bullet, std::allocator<Bullet> > > >::operator=(const std::reverse_iterator<__gnu_cxx::__normal_iterator<Bullet*, std::vector<Bullet, std::allocator<Bullet> > > >&) ../src/player.cpp:56: error: no match for ‘operator!=’ in ‘rii != ((Player*)this)->Player::bullets.std::vector<_Tp, _Alloc>::end [with _Tp = Bullet, _Alloc = std::allocator<Bullet>]()’ ../src/player.cpp:57: error: ‘class std::reverse_iterator<__gnu_cxx::__normal_iterator<Bullet*, std::vector<Bullet, std::allocator<Bullet> > > >’ has no member named ‘move’ ../src/player.cpp:58: error: no matching function for call to ‘std::vector<Bullet, std::allocator<Bullet> >::erase(std::reverse_iterator<__gnu_cxx::__normal_iterator<Bullet*, std::vector<Bullet, std::allocator<Bullet> > > >&)’ /usr/lib/gcc/i686-pc-linux-gnu/4.4.3/../../../../include/c++/4.4.3/bits/vector.tcc:133: note: candidates are: __gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> > std::vector<_Tp, _Alloc>::erase(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >) [with _Tp = Bullet, _Alloc = std::allocator<Bullet>] /usr/lib/gcc/i686-pc-linux-gnu/4.4.3/../../../../include/c++/4.4.3/bits/vector.tcc:145: note: __gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> > std::vector<_Tp, _Alloc>::erase(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, __gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >) [with _Tp = Bullet, _Alloc = std::allocator<Bullet>] ../src/player.cpp:60: error: ‘rri’ was not declared in this scope ../src/player.cpp:53: warning: unused variable ‘i’ make: *** [src/player.o] Error 1
Hmm...why did you make rii a reverse_interator? Could you just make it a normal iterator?
I don't document code. If it was hard to write, it should be hard to read
I tried before and got a segfault when trying to remove the object, I just tried doing it again with a normal iterator:
and, when compiled, got back error: ‘class __gnu_cxx::__normal_iterator<Bullet*, std::vector<Bullet, std::allocator<Bullet> > >’ has no member named ‘move’Code:for(i=bullets.begin(); i!= bullets.end(); i++){ if(!*i.move(deltaTicks)){ bullets.erase(i); }else{ *i.show(screen); } }
Be sure to wrap your de-reference of the iterator in parenthesis. The compiler will see the following line
It works if you wrap it like this.Code:*i.move(deltaTicks) // as the following line *(i.move(deltaTicks))
Code:(*i).move(deltaTicks)
I don't document code. If it was hard to write, it should be hard to read
I haven't looked at the code much, but the obvious problem I saw was that you're trying to make a reverse_iterator equal an iterator here:
bullets.begin() returns an iterator, not a reverse_iterator. You should use rbegin() and rend() instead.Code:std::vector<Bullet>::reverse_iterator rii; for(rii=bullets.begin(); rii!=bullets.end(); ++rii){
Your second problem is due to the order of operations. Dereferencing happens AFTER ".". It should look like this:
Code:if (!i->move(deltaTicks))
Wow I changed my sig!
Oh! I didn't even think about using the 'arrow' with the iterator. I have always just had to wrap the iterator in parenthesis and use the asterisk. Either way, it should work though.
I don't document code. If it was hard to write, it should be hard to read
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks