g++ compiler error with erase() on vector item

时间:2015-12-10 02:07:40

标签: c++ c++11 vector compiler-errors g++

I am having an odd compiler error and cannot solve it. I have provided the function where the error is flagged. I am using g++ as my compiler with -std=c++11.

Included: algorithm, vector, iterator libraries.

Error is thrown on 241.

Here is the code:

235 void callEditFn(vector<Media*> *medias, unsigned idx, bool del)
236 {
237         if (del)
238                 try
239                 {
240                         Media* item = medias->at(idx);
241                         medias->erase(medias->cbegin() + idx);
242                         cout << endl << "Item Deleted" << endl << item->toString() << ednl;
243                         delete item;
244                 }
245                 catch (out of range e)
246                 {
247                         cout << endl << "Item not Found! Nothing Deleted" << endl;
248                 }
249         }
250         else
251         {
252                 try
253                 {
254                         if (Book* b = dynamic_cast<Book*>(medias->at(idx)))
255                                 editBook(b);
256                         if(AudioTrack* a = dynamic_cast<AudioTrack*>(medias->at(idx)))
257                                 editAudioTrack(a);
258                         if(Movie* m = dynamic_cast<Movie*>(medias->at(idx)))
259                                 editMovie(m);
260                 }
261                 catch (out_of_range e)
262                 {
263                         cout << endl << "Item not Found! Nothing Edited" << endl;
264                 }
265         }
266 }

Here is the compiler error:

ct_tpms1.cpp: In function ‘void callEditFn(std::vector<Media*>*, unsigned int, bool)’:
ct_tpms1.cpp:241:40: error: no matching function for call to ‘std::vector<Media*>::erase(__gnu_cxx::__normal_iterator<Media* const*, std::vector<Media*> >)’
    medias->erase(medias->cbegin() + idx);
                                        ^
ct_tpms1.cpp:241:40: note: candidates are:
In file included from /usr/include/c++/4.8/vector:69:0,
                 from ct_tpms1.cpp:3:
/usr/include/c++/4.8/bits/vector.tcc:134:5: note: std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(std::vector<_Tp, _Alloc>::iterator) [with _Tp
= Media*; _Alloc = std::allocator<Media*>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<Media**, std::vector<Media*> >; typename std::_Vector_
base<_Tp, _Alloc>::pointer = Media**]
     vector<_Tp, _Alloc>::

0 个答案:

没有答案