Sorry for double posting but I kind of figured out that the error was due to nothing being returned from the catch block, and in the main function nothing was to be assigned to the test2 vector. I have modified the functions slightly:
Code:
const vector<short>& Input::getOneRow(int index) const
{
try
{
return values.at(index);
}
catch(const out_of_range &e)
{
cerr << "Out of range. Returning constant empty vector." << endl;
const static vector<short> bad;
return bad;
}
}
Now, due to efficiency reasons, I would like to return a constant reference to my vector and call it in this way:
Code:
int main()
{
Input myClass("someArgument");
const vector<short> &temp = myClass.getOneRow(5000);
}
The 5000 index is deliberately made to be out of range. So the values.at will throw an out_of_range exception, the catch block will be activated, and what should I return from it? I do not really like the way it is now, returning a static vector, since it is not really sensible to occupy the memory with something that is never going to be used anyway (constant reference to an empty static vector...). Futhermore, the static vector does not go out of scope so it is there forever. Is there a better way to deal with this problem?