Say I have an object containing a value. I wish to get the index of object with a particular value from a list of objects. I use the below code to do it,
int MyClass::getIndex(list& somelist, int requiredValue)
{
for( i=0; i != somelist.count(); ++i)
{
if(somelist.at(i)->value() == requiredValue)
return i;
else
continue;
}
return -1;
}
How to avoid the “doesn’t return a value on all code paths” warning without using an iterator?
You must return T from the function in any case. If the value possibly does not exists in the list you have options:
boost::optional<T>return end iterator:
Also
If you just want to find the iterator to some value, use
std::find:Finally
Don’t use list if you need access by index. Use vector in that case