将const_iterator传递给erase不能在gcc 4.7.1

时间:2016-01-20 20:13:11

标签: c++ c++11 vector iterator const-iterator

巨大的错误:

BlackJack.cpp: In member function âvoid BlackJack::leaveGame(const std::vector<int>&)â:
BlackJack.cpp:342:35: error: no matching function for call to âstd::vector<Player*>::erase(std::vector<Player*>::const_iterator&)â
BlackJack.cpp:342:35: note: candidates are:
In file included from /usr/local/gcc4.7/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.7.1/../../../../include/c++/4.7.1/vector:70:0,
                 from /usr/local/gcc4.7/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.7.1/../../../../include/c++/4.7.1/bits/random.h:34,
                 from /usr/local/gcc4.7/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.7.1/../../../../include/c++/4.7.1/random:50,
                 from /usr/local/gcc4.7/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.7.1/../../../../include/c++/4.7.1/bits/stl_algo.h:67,
                 from /usr/local/gcc4.7/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.7.1/../../../../include/c++/4.7.1/algorithm:63,
                 from BlackJack.cpp:8:
/usr/local/gcc4.7/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.7.1/../../../../include/c++/4.7.1/bits/vector.tcc:135:5: note: std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(std::vector<_Tp, _Alloc>::iterator) [with _Tp = Player*; _Alloc = std::allocator<Player*>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<Player**, std::vector<Player*> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = Player**]
/usr/local/gcc4.7/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.7.1/../../../../include/c++/4.7.1/bits/vector.tcc:135:5: note:   no known conversion for argument 1 from âstd::vector<Player*>::const_iterator {aka __gnu_cxx::__normal_iterator<Player* const*, std::vector<Player*> >}â to âstd::vector<Player*>::iterator {aka __gnu_cxx::__normal_iterator<Player**, std::vector<Player*> >}â
/usr/local/gcc4.7/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.7.1/../../../../include/c++/4.7.1/bits/vector.tcc:147:5: note: std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(std::vector<_Tp, _Alloc>::iterator, std::vector<_Tp, _Alloc>::iterator) [with _Tp = Player*; _Alloc = std::allocator<Player*>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<Player**, std::vector<Player*> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = Player**]
/usr/local/gcc4.7/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.7.1/../../../../include/c++/4.7.1/bits/vector.tcc:147:5: note:   candidate expects 2 arguments, 1 provided

相关功能:

static const vector<Player*>::const_iterator findPlayer(
        const vector<Player*>& players, int id) {///const
        vector<Player*>::const_iterator found
        = find_if(begin(players), end(players), [id] (Player* player) {
            return player->getId() == id;
        });
       return found;
    }

void BlackJack::leaveGame(const vector<int>& players){
        if(allPlayersIn(this->players_pool, players)){
            for(vector<int>::const_iterator cur_id = players.begin();
                    cur_id != players.end(); ++cur_id){
                vector<Player *>::const_iterator found =
                        findPlayer(this->_players_in, *cur_id);
                if(found != end(this->_players_in)){////end
                    this->_players_in.erase(found);
                    this->_players_in_count--;
                }
            }
        } else {
            PlayerDoesNotExistException();
        }
    }

有人知道为什么会这样吗? 据我所知,gcc ++ 11确实支持这个问题。 如果我删除所有“const”,编译器就不会喊出来。

0 个答案:

没有答案