Closed Thread
Results 1 to 6 of 6

Thread: C++ Vector Error

  1. #1
    matio's Avatar
    matio is offline Learning Programmer
    Join Date
    Aug 2009
    Location
    UK
    Posts
    51
    Rep Power
    0

    C++ Vector Error

    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:
    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);
    		}
    	}
    }
    when I try to compile it gives me this error:
    Code:
    ../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. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    cod3b3ast's Avatar
    cod3b3ast is offline Learning Programmer
    Join Date
    Dec 2009
    Posts
    74
    Rep Power
    0

    Re: C++ Vector Error

    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

  4. #3
    matio's Avatar
    matio is offline Learning Programmer
    Join Date
    Aug 2009
    Location
    UK
    Posts
    51
    Rep Power
    0

    Re: C++ Vector Error

    I tried before and got a segfault when trying to remove the object, I just tried doing it again with a normal iterator:
    Code:
    	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’

  5. #4
    cod3b3ast's Avatar
    cod3b3ast is offline Learning Programmer
    Join Date
    Dec 2009
    Posts
    74
    Rep Power
    0

    Re: C++ Vector Error

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

    Code:
    *i.move(deltaTicks)
    // as the following line
    *(i.move(deltaTicks))
    It works if you wrap it like this.

    Code:
    (*i).move(deltaTicks)
    I don't document code. If it was hard to write, it should be hard to read

  6. #5
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: C++ Vector Error

    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:
    Code:
    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:
    Code:
    if (!i->move(deltaTicks))
    Wow I changed my sig!

  7. #6
    cod3b3ast's Avatar
    cod3b3ast is offline Learning Programmer
    Join Date
    Dec 2009
    Posts
    74
    Rep Power
    0

    Re: C++ Vector Error

    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

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. getting a 'vector subscript out of range' error
    By squid attack in forum C and C++
    Replies: 3
    Last Post: 08-03-2010, 04:34 PM
  2. Vector or dir+vel?
    By Walle in forum C and C++
    Replies: 19
    Last Post: 01-07-2010, 07:30 PM
  3. General what is vector
    By h4x in forum Assembly
    Replies: 29
    Last Post: 09-04-2009, 05:07 AM
  4. Replies: 2
    Last Post: 07-10-2009, 11:36 AM
  5. Initializing a vector
    By Kuban in forum C and C++
    Replies: 1
    Last Post: 10-19-2008, 05:25 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts