简体   繁体   中英

using iterator to erase element in vector

I have the following vector to store elements of type player:

std::vector<player> players;

in a class called game which has the following function:

void game::removePlayer(string name) {
  vector<player>::iterator begin = players.begin();

  // find the player
  while (begin != players.end()) {
      if (begin->getName() == name) {
          break;
      }
      ++begin;
  }

  if (begin != players.end())
      players.erase(begin);

}

I get the following error:

    1>------ Build started: Project: texas holdem, Configuration: Debug Win32 ------
1>  game.cpp
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(2514): error C2582: 'operator =' function is unavailable in 'player'
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(2535) : see reference to function template instantiation '_OutIt std::_Move<_InIt,_OutIt>(_InIt,_InIt,_OutIt,std::_Nonscalar_ptr_iterator_tag)' being compiled
1>          with
1>          [
1>              _OutIt=player *,
1>              _InIt=player *
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(1170) : see reference to function template instantiation '_OutIt std::_Move<player*,player*>(_InIt,_InIt,_OutIt)' being compiled
1>          with
1>          [
1>              _OutIt=player *,
1>              _InIt=player *
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(1165) : while compiling class template member function 'std::_Vector_iterator<_Myvec> std::vector<_Ty>::erase(std::_Vector_const_iterator<_Myvec>)'
1>          with
1>          [
1>              _Myvec=std::_Vector_val<player,std::allocator<player>>,
1>              _Ty=player
1>          ]
1>          c:\vcprojects\texas holdem\texas holdem\game.h(29) : see reference to class template instantiation 'std::vector<_Ty>' being compiled
1>          with
1>          [
1>              _Ty=player
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

removing the line

players.erase(begin);

fixes the error, why is it happening though and how can I fix it?

You need to overload the assignment operator Player & operator= (const Player & other) for your class Player. This required because erase requires it's argument to be copyable (it needs to rearrange the other elements of the vector after removal).

What's happening is that the library code removes the player object by shoving each of the array elements above that iterator down one slot. To do that, it copies each object, using operator=. Apparently the class player doesn't have that operator.

The problem is that your Player class is not movable. In order to remove a Player from the middle of the vector, all the Player s after that have to be moved down one space in the vector. One solution is not to use a vector. Another is to make Player movable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM