Iterating through a vector?
Hello everyone.
I have been getting an std:out_of_range error writing code that would iterate through a vector�*main.grid(), and pushing an element index onto an element in main.grid.
Here's my code:
Main.grid is resized to divisions squared, but this shouldn't matter. In this case divisions is 20.
When I run gdb opens, prints:
I can't see why this is happening.
Can someone please help me?
Thanks
I have been getting an std:out_of_range error writing code that would iterate through a vector�*main.grid(), and pushing an element index onto an element in main.grid.
Here's my code:
Code:
int cell = 0;
while ( cell < main.grid.size() ) // for every cell in the array...
{
if (triangleBox2D(cellbox,current) == 1) // This function always returns 1
{
cout << "sorting triangle " << index/9 << " into cell " << cell << "\n";
cout << "accessing element " << cell << " from an array of size "
<< main.grid.size() << " and pushing back on an array of size "
<< main.grid.at(cell).list.size() << "\n";
main.grid.at(cell).list.push_back(index); // add this triangles index to the end of the list of triangles for this cell.
// make sure there is a main.grid.at(cell)
}
cell++;
}Code:
main.grid.resize((divisions)*(divisions));Main.grid is resized to divisions squared, but this shouldn't matter. In this case divisions is 20.
When I run gdb opens, prints:
Quote:accessing element 397 from an array of size 400 and pushing back on an array of size 31
sorting triangle 31 into cell 398
accessing element 398 from an array of size 400 and pushing back on an array of size 31
sorting triangle 31 into cell 399
accessing element 399 from an array of size 400 and pushing back on an array of size 31
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check
I can't see why this is happening.
Can someone please help me?
Thanks
~ Bring a Pen ~
What are the data type elements of your grid vector? My C++ is a little rusty, but is there a chance that the elements do not have a default constructor defined? My understanding is the vector class on resize will fill in the missing values using the default constructor. But if there is none defined, maybe this causes a problem?
Sorry, 'grid' is a vector of vectors (achieved through classes again) and then 'cell' is a vector of integers. I didn't create default constructors or destructors. Should I?
~ Bring a Pen ~
vector requires that the type it contains have the following:
As for the code itself, it looks like the accessing of the grid vector seems fine. Are you sure that something isn't using cell after that loop to access the vector?
- Default constructor
- Destructor
- operator=
- copy constructor
As for the code itself, it looks like the accessing of the grid vector seems fine. Are you sure that something isn't using cell after that loop to access the vector?
Could there be a problem with adding to the end of a vector?
Since I are using the last element in main.grid, that is in this case 399, could there be a problem with pushing onto it?
When I push onto the last item in the sub-array of main.grid (list) does this somehow go past the size of main.grid?
Since I are using the last element in main.grid, that is in this case 399, could there be a problem with pushing onto it?
When I push onto the last item in the sub-array of main.grid (list) does this somehow go past the size of main.grid?
~ Bring a Pen ~
No, that is impossible. It is modifying the object at main.grid[cell], not the vector main.grid. You should put a printout right after the loop to double check if it crashes in the loop or if it's crashing sometime after it. Looking at that code, there doesn't seem to be any out of range accesses.
Ahah! There is a small access to the vector after the function, although it worked before. Thanks anyway, and sorry for being a waste of time
~ Bring a Pen ~

