Jump to content

C++ Vector Error

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
matio

matio

    Learning Programmer

  • Members
  • PipPipPip
  • 51 posts
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:

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);

		}

	}

}


when I try to compile it gives me this error:

../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


BTW: the code was taken from a tutorial on vectors and modified by me, also all the functions that are called exist

#2
thechef

thechef

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
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 ;)

#3
matio

matio

    Learning Programmer

  • Members
  • PipPipPip
  • 51 posts
I tried before and got a segfault when trying to remove the object, I just tried doing it again with a normal iterator:
	for(i=bullets.begin(); i!= bullets.end(); i++){
		if(!*i.move(deltaTicks)){
			bullets.erase(i);
		}else{
			*i.show(screen);
		}
	}

and, when compiled, got back error: ‘class __gnu_cxx::__normal_iterator<Bullet*, std::vector<Bullet, std::allocator<Bullet> > >’ has no member named ‘move’

#4
thechef

thechef

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
Be sure to wrap your de-reference of the iterator in parenthesis. The compiler will see the following line

*i.move(deltaTicks)
// as the following line
*(i.move(deltaTicks))

It works if you wrap it like this.

(*i).move(deltaTicks)

I don't document code. If it was hard to write, it should be hard to read ;)

#5
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
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:
std::vector<Bullet>::reverse_iterator rii;
for(rii=bullets.begin(); rii!=bullets.end(); ++rii){
bullets.begin() returns an iterator, not a reverse_iterator. You should use rbegin() and rend() instead.

Your second problem is due to the order of operations. Dereferencing happens AFTER ".". It should look like this:
if (!i->move(deltaTicks))

Wow I changed my sig!

#6
thechef

thechef

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
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 ;)