0

What's the error in compiling const iterator

Please refer code below: Can anyone suggest how to remove the errors ? https://code.sololearn.com/c2pTQZJ1I4NE/?ref=app

15th Feb 2022, 9:48 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
4 ответов
+ 2
It's not an error because the const_iterator returns a 'Test* const', not a 'const Test*'. Which is pretty much the same as if you return const int from a function, as in you can still assign it to a non const int. If you want it to be const either have a container of 'const Test*' objects or explicitly add const on line 32.
16th Feb 2022, 9:14 AM
Dennis
Dennis - avatar
+ 1
Thanks Brian nd Dennis
16th Feb 2022, 3:58 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
The first error seems to get corrected if I change line 40 from itr->display(); To (*itr)->display(); After that, errors are due to attempts to access freed and then nulled object pointers.
16th Feb 2022, 2:17 AM
Brian
Brian - avatar
0
Thanks Brian That's correct.. when I solve the *itr issue, I am getting memory dump error. If I have taken const iterator in first for loop and passing pointer by reference to trial(p); Should not it be a compile time error ? why error : bcz const iterator should not allow modification but passing it by reference does not guarantee no modification
16th Feb 2022, 3:55 AM
Ketan Lalcheta
Ketan Lalcheta - avatar