0

我有以下向量来存储播放器类型的元素:

std::vector<player> players;

在一个名为 game 的类中,它具有以下功能:

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

}

我收到以下错误:

    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 ==========

删除线

players.erase(begin);

修复了错误,为什么会发生,我该如何解决?

4

3 回答 3

1

您需要Player & operator= (const Player & other)为您的类 Player 重载赋值运算符。这是必需的,因为erase要求它的参数是可复制的(它需要在删除后重新排列向量的其他元素)。

于 2012-08-14T00:40:29.103 回答
1

正在发生的事情是库代码player通过将迭代器上方的每个数组元素推到一个槽中来删除对象。为此,它使用 operator= 复制每个对象。显然该类player没有该运算符。

于 2012-08-14T00:41:59.163 回答
0

问题是您的Player课程不可移动。为了Player从向量的中间移除 a,Player之后的所有 s 都必须在向量中向下移动一个空格。一种解决方案是不使用向量。另一个是制作Player可移动的。

于 2012-08-14T00:40:54.280 回答