Error: the conversion from ‘STD: : _List_const_iterator & lt; _Mylist> To non-scalar Type ‘STD ::_List_iterator< _Mylist> ‘the requested
Error C2440 resolved: “Initialize” : cannot be retrieved from “STD ::_List_const_iterator< _Mylist & gt;” Into “STD: : _List_iterator & lt; _Mylist & gt;”
Writing C++ code often USES const as a function argument, which is easy to do when using iterator if the variable is of type STL or contains type STL.
void list_print(const list<int> &list)
{
for (list<int>::iterator iter = list.begin();
iter != list.end();
++iter) {
...
}
}
The following error will be reported in this case
error C2440: “initialize”: cannot “std::_List_const_iterator<_Mylist>” to “std::_List_iterator<_Mylist>”
or
error: conversion from 'std::_List_const_iterator<_Mylist>' to non-scalar type 'std::_List_iterator<_Mylist>' requested
Here, because list itself is of type const, we need to use a const-type iterator, which is list::const_iterator
Code to
void list_print(const list<int> &list)
{
for (list<int>::const_iterator iter = list.begin();
iter != list.end();
++iter) {
...
}
}
done