This piece of code compiles file in VC6 but in VS 2008 it gives an error. Can anyone tell me why?
I guess it is because you can no longer compare a pointer to NULL (which is a typedef for 0).
If that is the case, how do I do this comparison in VC9?
for ( std::vector<aCattrBase*>::iterator iT = attrLst.begin(); iT < attrLst.end(); iT++)
{
if ( (iT != NULL) && (*iT != NULL) ) //Error: C2678
{
//code
}
}
error C2678: binary ‘!=’ : no operator
found which takes a left-hand operand
of type
‘std::_Vector_iterator<_Ty,_Alloc>’
(or there is no acceptable conversion)
The type for ‘std::vector::iterator’ is not necessarily a pointer type so you can not compare it to NULL.
In your old compiler it just happened to be a pointer and so your code compiled. But you just got lucky (as shown when you moved the code to a different compiler).
The only test on iterator you have is to compare it to end() or begin() or any valid iterator within the range begin() -> end(). Since this is a vector you can do mathematical operations with the iterator. iT-begin() should give you an offset. But this is not valid for all containers (check each containers documentation).
All you need to do is test what the iterator points at: