Hi all,
This is my maiden post on not only this forum, but in fact any programming forum so please bear with me. I must say that this question does relate to a piece of homework, but it is the final member function in a series of about 8 and I will be posting all of my own code for you.
OK. The specs required me to read in a file which consisted of a number of rows and a number of columns, as well as a series of 0s and 1s (stored as bools). I was provided with a header file which contained a series of member functions to be implemented as well as three private data members:
1 - 2 variables to store rows and columns, respectively
2 - a 2d vector of vector< bool> - grid <vector <bool> >
The final member function rotates a "bitmap", which for the purposes of this assignment is a very simple piece of ascii art. Here is the problem:
*Everything* works, except for the fact that when I try to copy the contents of a temporary 2d vector I have created to manipulate the 1d vector, it fails and throws a range error. Let me show you the code:
void bitmap::rotate()
{
vector<vector <bool> > Vec; // vector created for the purpose of rotating contents of grid
vector <bool> Vec1; // 1d vector for pushing each column of original vector onto Vec temporarily before assigning back to grid
for (int i = 0; i < numcols; i++) // outer loop controls column number which will stay the same since we are cycling through the rows in this case
{
for (int j = 0; j < numrows; j++) // inner loop cycles through rows, keeping column the same
{
Vec1.push_back(grid.at(numrows - 1 - j).at(i)); // push the value at the bottom left of original vector onto top left of the new vector
}
Vec.push_back(Vec1); // push entire new 1d vector onto Vec
Vec1.clear(); // clear Vec1 so that it is ready for next iteration
}
for (int k = 0; k < numcols; k++) // this is simply for debugging to demonstrate that Vec contains the correctly rotated image
{
for (int l = 0; l < numrows; l++)
{
if (Vec.at(k).at(l) == 0)
cout << " ";
else
cout << "*";
}
cout << endl;
}
grid = Vec;
}
I am really at the end of my tether because I can not think of a single thing to try. I have tried clearing grid and resizing and then copying the individual elements over. This also did not work. Any help would be extremely greatly appreciated. If I have posted too much information or in an annoying format, please tell me so that I do not make the same mistake twice.
Kind regards,
Daniel