擦除元件I具有以下矢量存儲類型播放機的元素:使用迭代向量
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);
}
:std::vector<player> players;
在一類稱爲遊戲,其具有以下功能
我收到以下錯誤:
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);
修正錯誤,爲什麼會發生,雖然,我該如何解決?
作爲樣式說明,我會爲迭代器選擇「begin」以外的名稱,因爲我相信讀者很容易將其與着名的名稱「begin()」混淆。例如,當我看到「players.erase(begin)」這一行時,「我的第一個想法是,」他爲什麼要擦除第一個元素而不是匹配元素?「。 – 2012-08-14 00:59:24