getting "vector iterators incompatible"

Hi,

Currently I am working in visual Studio 2008. Previously I was working in VS 2003 but I migrated the code VS2008.
I am getting below error in VS 2008 which was NOT in vS2003.
Error: _DEBUG_ERROR("vector iterators incompatible");

When i searched on the internet I found that it is due to strict type checking of vs2008.

Below is code snippet where I am getting run time error

bool CardCol::Contains(const TAMCardPtr pCard){ //collection class method
bool contains=containedincards_if(begin(),end(),bind2nd_ref(mem_fun(&CardCol::IsIdentical),pCard));
return contains;
}

template<class _InIt, class _Fn1> inline
bool containedincards_if(_InIt _First, _InIt _Last, _Fn1 _Func) {
return _Last!=findincards_if(_First,_Last,_Func); <-- error [("vector iterators incompatible")] while != comparsion
}

template<class _InIt, class _Fn1> inline
_InIt findincards_if(_InIt _First, _InIt _Last, _Fn1 _Func) {
for (; _First != _Last; ++_First){
if (_Func(*_First))
break;
if((*_First)->HasSlaveCards()){

_InIt itSlave;
for (itSlave=(*_First)->GetSlaveCards()->begin();itSlave!=(*_First)->GetSlaveCards()->end();++itSlave){
if (_Func(*itSlave))
break;
}
if (itSlave!=(*_First)->GetSlaveCards()->end())
return itSlave;
}
}
return _First;
}

Here I guess the failure is due to comparison of iteraotrs two different vectors of same type.

PLease help .

K2R
First and foremost: Use CODE tags when posting code.

Second, I see little use in the comparison of two iterators from two different vectors. If you want to know if an object is contained in another vector, then the best way would be to overload the operator== and do the object comparison directly instead of trying to compare iterators.
Alternatively, make findincard_if return a bool as it knows what iterators should be compared.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
template<class _InIt, class _Fn1>
inline
boo findincards_if(_InIt _First, _InIt _Last, _Fn1 _Func)
{
    for (; _First != _Last; ++_First)
    {
        if (_Func(*_First))
            break;
        if((*_First)->HasSlaveCards())
        {
            _InIt itSlave;
            for (itSlave=(*_First)->GetSlaveCards()->begin();itSlave!=(*_First)->GetSlaveCards()->end();++itSlave)
            {
                if (_Func(*itSlave))
                    break;
            }
            if (itSlave!=(*_First)->GetSlaveCards()->end())
                return true;
        }
    }
    return _First != _Last;
}
Last edited on
@webJose :
I did overloading of == operator. Now i am geeting "vector iterator not dereferencable" at bold line(pCardLast=TAMCardPtr::DynamicCastEx(*_Last))

1
2
3
4
5
6
7
bool findincards_if(_InIt _First, _InIt _Last, _Fn1 _Func) {
	TAMCardPtr pCardFirst,pCardLast;
    for (; _First != _Last; ++_First){
		pCardFirst=TAMCardPtr::DynamicCastEx(*_First);
		pCardLast=TAMCardPtr::DynamicCastEx(*_Last);
        if (_Func(*_First))
            break;

it seems that the _Last is not coming properly(means empty..null ??).
But the the value of _Last is taken from end() of collection.
How it could be failing?
Can you suggest any alternative way of doing same thig?

@kbw :

The solution you suggested is NOT working.
Dunno, ... it's your code.
Topic archived. No new replies allowed.