T
Tricky
Using std::vector, I am seeing the assertion:
"vector iterator not dereferencable"
I know exactly where the problem is but am not sure how to work around it.
The error occurs in a function like this:
(This is not the actual code that I'm posting, which I know is discouraged,
but it would just be too much to ask everyone to wade through... It's a
fairly complex text parser with lots of recursion, etc.)
----------
bool myclass_1::match(std::vector<myclass_2 *>::iterator &pos)
{
if ((*pos)->x == x) // x is just some member of both myclass_1 and 2
{
pos++;
return true;
}
return false;
}
----------
The assertion fails because it's possible that "pos" will be equal to
vector::end() by the time it reaches this function. Logically, that is
acceptable, and the function should just return false in that case.
So, the question is, how can I check "pos" to see if it is invalid before
trying to dereference it?
I guess that I could pass a reference to the vector from which pos was
created into the "match" function... But there are many, many of these
match functions and the vector itself just isn't used in them. So it seems
like unnecessary overhead just to provide access to the "end()" function.
Is that the only solution?
"vector iterator not dereferencable"
I know exactly where the problem is but am not sure how to work around it.
The error occurs in a function like this:
(This is not the actual code that I'm posting, which I know is discouraged,
but it would just be too much to ask everyone to wade through... It's a
fairly complex text parser with lots of recursion, etc.)
----------
bool myclass_1::match(std::vector<myclass_2 *>::iterator &pos)
{
if ((*pos)->x == x) // x is just some member of both myclass_1 and 2
{
pos++;
return true;
}
return false;
}
----------
The assertion fails because it's possible that "pos" will be equal to
vector::end() by the time it reaches this function. Logically, that is
acceptable, and the function should just return false in that case.
So, the question is, how can I check "pos" to see if it is invalid before
trying to dereference it?
I guess that I could pass a reference to the vector from which pos was
created into the "match" function... But there are many, many of these
match functions and the vector itself just isn't used in them. So it seems
like unnecessary overhead just to provide access to the "end()" function.
Is that the only solution?