yuval
yuval

Reputation: 3088

can not use the list iterator in c++

ok so i have errors in my code where i include an iterator.
here is the buggy part of my code:

for(list<char>::iterator it = eatUpRight.begin();it!= eatUpRight.end();it+=2)
{
    board[*it][*(it+1)]=3;
    _3eat2(*it,*(it+1),eatOptions,newCurrentEatingOption);
    board[*it][*(it+1)]=0;
}

don't worry about the board and _3eat2 and any other identifier because that is defentlly not the problem.
all you need to know is that board is a 2d array.

board[*it][*(it+1)]

the (it) is supose to be the index of the 2d array,but it gives me errors instead. and gives me other errors on everywhere else that i try to use the iterator.
so can you please tell me whats wrong with this code?

Upvotes: 1

Views: 136

Answers (3)

steffen
steffen

Reputation: 8988

Additional remark: it is not an index but an iterator. Amoong other things that means that you cannot use it on a different collection than the one that gave it to you.

You are receiving it from eatUpRight and are using it (as an index!) with board.

Upvotes: 0

ForEveR
ForEveR

Reputation: 55897

std::list<T>::iterator 

is a bidirectional iterator. It has no operator +(std::ptrdiff_t); use std::advance instead of operator +

Upvotes: 1

pmr
pmr

Reputation: 59841

it + 1 is only valid for RandomAccessIterators. list doesn't provide RandomAccessIterators, but BidirectionalIterators. See an overview of the iterator library here. Use std::advance, to abstract away the difference of those operations.

Upvotes: 6

Related Questions