Iterating through a vector?

Member
Posts: 281
Joined: 2009.04
Post: #1
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:



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 ~
Quote this message in a reply
Member
Posts: 43
Joined: 2009.10
Post: #2
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?
Quote this message in a reply
Member
Posts: 281
Joined: 2009.04
Post: #3
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 ~
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #4
vector requires that the type it contains have the following:
  • Default constructor
  • Destructor
  • operator=
  • copy constructor
The compiler generates defaults for all of the above. You do not need to implement them yourself if your class only contains primitives and objects by value that also satisfy the above requirements. If you have pointers or objects by value that don't satisfy those requirements, you will need to implement them yourself.

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?
Quote this message in a reply
Member
Posts: 281
Joined: 2009.04
Post: #5
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?

~ Bring a Pen ~
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #6
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.
Quote this message in a reply
Member
Posts: 281
Joined: 2009.04
Post: #7
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 Smile

~ Bring a Pen ~
Quote this message in a reply
Post Reply