I have the following code:
vector<vector<Obj> > vectObjVects(10);
vectObjVects[0].reserve(100);
During runtime, at the third line of my code, I get a ‘Fatal Error’ thrown by the line _SCL_SECURE_VALIDATE_RANGE(_Pos < size());” in vector standard header which is presumably because it doesn’t like me trying to index into vectObjVects before a size has been set.
What is the recommended way to reserve space in the inner vectors of a 2D-vector before any values have been filled in?
EDIT – Obj class added as per request:
class Obj
{
public:
Obj(int a, int b):iA(a), iB(b) {}
Obj():iA(0), iB(0) {}
int iA;
int iB;
};
There appear to be no errors in the code you have posted (see below).
The most likely explanations of your issue are:
My suggestion is that you use the debugger to examine the state of
vectObjVectsjust before the call toreserve. If that looks OK, step into the code for reserve and see what the values of_Posandsize()are. They should be0and10– if they’re not then that should point you in the right direction.The following code compiles and runs exactly as expected: