My program raises a "vector subscript out of range" exception (EDIT: assertion) on a return statement. Well, it seems like it since it raises it exactly on that breakpoint.
Here is the function that causes it :
Matrix4 Perspective(float fov, float aspect, float near, float far) const {
double yScale = 1.0 / tan(TO_RADIANS * fov / 2);
double xScale = yScale / aspect;
double depth = near - far;
Matrix4 perspective;
perspective[0][0] = xScale;
perspective[1][1] = yScale;
perspective[2][2] = (far + near) / depth;
perspective[2][3] = 2 * far * near / depth;
perspective[3][2] = -1;
perspective[3][3] = 0;
return perspective; // Raises exception here?
}
My default Matrix4
constructor is fine, it basically does this:
_matrix.resize(4);
for (unsigned int i = 0; i < 4; ++i)
_matrix[i].resize(4);
_matrix
being a std::vector<std::vector<float>>
attribute. So everything is set to 0.
Finally, the piece of code that uses the result of the return statement is this:
Matrix4 camera = Perspective(70, 1, 0.2, 10);
And I have a copy constructor which looks fine aswell:
Matrix4(const Matrix4& matrix) {
_matrix.reserve(4);
for (unsigned int i = 0; i < 4; ++i) {
_matrix[i].reserve(4);
for (unsigned int j = 0; j < 4; ++j)
_matrix[i][j] = matrix[i][j];
}
}
(I also have an overloaded operator[] but the problem really cannot be caused by it.)
The exception assertion seems to be raised on the return perspective;
, but maybe it is raised by the line of code that called the method, and the copy constructor somehow failed to copy? But in that case, Visual Studio should bring me inside the constructor since I'm using detailed step-by-step...
I'm lost at this point...