Uys of Spades
Uys of Spades

Reputation: 171

Trouble with List Iterators Incompatible in STL

Ok I am, average at programming, I am not a professional, but I do know enough to get me around. I am having trouble with this piece of code, btw I am using Microsoft Visual 2013. Pretty much what it is saying to me its giving me a very bad bug saying that the list iterators is incompatible. So basically it doesn't like that I am comparing one thing from one list to another thing from another list. My work around was extracting both 'values' and inputting them into a different list where then I will compare the two, but that may be the hard way of doing things. Can anyone please shed some light on this for me? Thanks!!

list<string>::iterator cur;
list<string>::iterator dis;

cur = current_list.begin();
dis = disregard_list.begin();
unsigned int x = 1;
while (x < current_list.size()  )
{

    if (cur == dis)
    {
        cur = current_list.erase(cur);
    }
    else
    {
        ++cur;
        ++x;
    }
}

Upvotes: 0

Views: 265

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 311126

Iterators cur and dis belong to different lists

cur = current_list.begin();
dis = disregard_list.begin();

So you may not compare them

if (cur == dis)

Maybe you meant something as

if ( *cur == *dis)

Also it seems that this loop

unsigned int x = 1;
while (x < current_list.size()  )

is invalid. Should the initial value of x be equal to 0?

Take into account that class std::list has method remove Maybe you meant

current_list.remove( disregard_list.front() );

Upvotes: 1

Related Questions