Good day to all! I'm experiencing some issue with my custom 'vector' template class that I'm required to built to understand how the mechanism of a STL vector work. However one of the template function kept returning a 'control may reach end of non-void function. A quick search on the error and mainly its because the compiler is expecting a 'return'. But I've already achieved that. I'm using Xcode v9.4.1
You could do that. Or you could just have a return at the end on its own:
1 2 3 4 5 6 7 8 9 10 11 12
template <class elemType>
elemType& Vector<elemType>::at(int i)
{
if ((i >= 0) && (i<length)) {
return list[i];
}
else
{
// Uh oh - what do you do here?
return ... // return what?
}
}
or
1 2 3 4 5 6 7 8 9 10
template <class elemType>
elemType& Vector<elemType>::at(int i)
{
if ((i >= 0) && (i<length)) {
return list[i];
}
// Uh oh - what do you do here?
return ... // return what?
}
Either of these will fix the problem. But what should you return? If someone using your vector class asks for contents of element 50 , but the vector doesn't have an element 50 because the vector is smaller than that, what should you return? This is something you need to think about.