• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with list:erase

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good day.

We have this exercise wherein we edit and delete stuff from a list. So far, i've done the editing part but i cant seem to understand how the syntax for list:erase works.

Here's my code for the deletion function:



My problem lies within the for-loop. to be exact, it's this part:



dev-c++ gives me these two notes regarding that part:

note C:\Dev-Cpp\include\c++\3.4.2\bits\list.tcc:96 candidates are: typename std::list<_Tp, _Alloc>::iterator std::list<_Tp, _Alloc>::erase(std::_List_iterator<_Tp>) [with _Tp = Student, _Alloc = std::allocator<Student>]

note C:\Dev-Cpp\include\c++\3.4.2\bits\list.tcc:96 std::_List_iterator<_Tp> std::list<_Tp, _Alloc>::erase(std::_List_iterator<_Tp>, std::_List_iterator<_Tp>) [with _Tp = Student, _Alloc = std::allocator<Student>]

i've tried removing the asterisk from the iterator (i.e. deletion = w) and also tried to copy the syntax of the example from list::erase C++ (w = input.erase(w)) but sadly both of them didnt work.

im sorry if this looks like a rookie question. the lecturer's approach was more hands on based. he didnt lectured much, just gave exercises for us to do and answered any questions we have. currently, im depending more on the internet and books (i.e. self-study).

any help is appreciated.
 
author
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jose Kampilan wrote:


i've tried removing the asterisk from the iterator (i.e. deletion = w)



That won't work: deletion is of type Student, whereas w is an iterator.

Jose Kampilan wrote:
and also tried to copy the syntax of the example from list::erase C++ (w = input.erase(w)) but sadly both of them didnt work.



What do you mean by "didn't work" in this case? w=input.erase(w) is what you should use here. However, this updates w to refer to the next element, so the w++ in the loop header is now wrong, and will skip elements, or move off the end of the list.

What you need is to only increment w if the entry was not found:




 
Jose Kampilan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it worked!

Thanks lots

That won't work: deletion is of type Student, whereas w is an iterator.



i thought since *w is pointing to an object, it can be contained in a type Student variable (i've used the same somewhere else in my code and it worked).

i guess i still have a lot to learn in C++.
reply
    Bookmark Topic Watch Topic
  • New Topic