| Author |
Help with list:erase
|
Jose Kampilan
Greenhorn
Joined: Aug 04, 2012
Posts: 11
|
|
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.
|
 |
Anthony Aj Williams
author
Ranch Hand
Joined: Jun 10, 2011
Posts: 56
|
|
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:
|
Author of C++ Concurrency in Action http://www.stdthread.co.uk/book/
just::thread C++11 thread library http://www.stdthread.co.uk
Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk
15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976
|
 |
Jose Kampilan
Greenhorn
Joined: Aug 04, 2012
Posts: 11
|
|
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++.
|
 |
 |
|
|
subject: Help with list:erase
|
|
|