我有一段代码,我首先定义
std::vector<int> half_gid_m;
我通过函数push_back计算向量half_gid_m的元素。 然后我在向量上执行函数sort和unique:
sort(half_gid_m.begin(),half_gid_m.end());
std::vector<int>::iterator itv( std::unique(half_gid_m.begin(),half_gid_m.end()) );
最后,我用
重复输入来删除矢量的一部分half_gid_m.erase(itv,half_gid_m.end());
似乎一切都是正确的,但编译给了我这种奇怪的错误
error: no matching function for call to 'std::vector<int, std::allocator<int> >::erase(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >&, __gnu_cxx::__normal_iterator<const int*, std::vector<int, std::allocator<int> > >&) const'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/vector.tcc:110: note: candidates are: typename std::vector<_Tp, _Alloc>::iterator 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 = int, _Alloc = std::allocator<int>]
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/vector.tcc:122: note: typename std::vector<_Tp, _Alloc>::iterator 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 = int, _Alloc = std::allocator<int>]
//home/gales/ingv/p-gales/simulations/little_mesh/lib/p-gales/include/gales-0.1/gales/map/map_rule.hpp:166: error: no matching function for call to 'std::vector<int, std::allocator<int> >::erase(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >&, __gnu_cxx::__normal_iterator<const int*, std::vector<int, std::allocator<int> > >&) const'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/vector.tcc:110: note: candidates are: typename std::vector<_Tp, _Alloc>::iterator 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 = int, _Alloc = std::allocator<int>]
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/vector.tcc:122: note: typename std::vector<_Tp, _Alloc>::iterator 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 = int, _Alloc = std::allocator<int>]
我真的无法理解问题所在,因为vector,push_back,sort和unique操作是以正确的方式编写的。
有人能帮助我吗?
答案 0 :(得分:5)
实质上它说error: no matching function for call to 'std::vector::erase(...) const
。即你在一个常量向量上调用erase
,而erase
是一个非const成员函数。使向量非常量。