Literal translation iterator type is incompatible
The problem that my colleague met today is a question of habitual wording. Describe the code:
struct track{};
Class Basetrack
{
...
Std::vector<track> gettracksourcelist ();
...
};
The problem lies in:
void func (Basetrack bt)
Std::vector<track>::iterator it = BT. Gettracksourcelist (). Begin ();
for (; it! = Bt. Gettracksourcelist.end (); ++it)
Focus is the red part, the value is not a reference, no get once is a stack of space, can not be compared. But the errors reported here (Iterators are incompatible) are not very understandable.
There are many articles on the Internet, although not the cause of the problem today, but also can be very good for reference.
To intercept parts of which are of reference value:
void _compat (const _myiter& _right) const
{//test for compatible iterator pair
if (this->_getcont () = = 0//Determine whether _myproxy is 0, 0 is an error, otherwise get the owning container
|| This->_getcont ()! = _right._getcont ())//Judging whether the type class of two vectors is consistent
{//Report error
_debug_error ("Vector iterators incompatible");
_scl_secure_invalid_argument;
}
}
Const _CONTAINER_BASE12 *_getcont () const
{//Get owning container
return (_myproxy = = 0? 0: _myproxy->_mycont);
}
Here I find that the _myproxy in my code is 0, which means that our type should be no problem, but the "chain" of the vector is broken.
From the tracking of the library, I find that the vectors are using two pointers named "_myproxy" and "_mynextiter" to find the adjacent values, and when we define a vector, it initializes a "_myproxy"
Workaround: Change to a pass-by reference. Or use an array to store the get information , and then set the array back to the end of the operation.
There are also many errors caused by invalid iterators when erase is removed, workaround: Use the return value of erase to record the next position of the iterator.
Reference Address:
http://blog.csdn.net/olanmomo/article/details/38420907
Http://stackoverflow.com/questions/8421623/vector-iterators-incompatible
Vector iterators Incompatible