How do I remove this error

error: invalid operands to binary expression (‘std::list::const_iterator’ (aka
‘_List_const_iterator’) and ‘std::list::const_iterator’
(aka ‘_List_const_iterator’))

for (; Li!=Lend; ++Li, ++Ni)

error: cannot increment value of type ‘std::list::const_iterator’ (aka

for (; Li!=Lend; ++Li, ++Ni)

The part of code:
if (L.size()!=Ne.size()) abort();
std::list::const_iterator Li (L.begin());
std::list::const_iterator Lend(L.end());
std::list::const_iterator Ni (Ne.begin());

for (; Li!=Lend; ++Li, ++Ni) {…}

Use instead:
std::list::iterator Li (L.begin());

Or see for loop - How to iterate through a list of objects in C++? - Stack Overflow

Actually this was the part of the code :slight_smile:
if (L.size()!=Ne.size()) abort();
std::list double ::const_iterator Li (L.begin());
std::list double ::const_iterator Lend(L.end());
std::list double ::const_iterator Ni (Ne.begin());

for (; Li!=Lend; ++Li, ++Ni) {

I tried what you recommended, but it didn’t work

Both for Li and Ni, you need to remove the “const” when defining it. You can’t define something as constant, and then change its value (++Li, ++Ni).

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.