mercredi 25 mars 2020

Why smart pointer cause std::length_error

Hello i am use in my code smart pointer and when i try compile my program , visual studio redirects me to the vector class and a popup pops up with this notification:

Unhandled exception at location 0x00007FFD725A9179 in game of life.exe: Microsoft C ++ exception: std :: length_error at memory location 0x00000004A491E9B0.

my code:

enter code here
template <class cellDerived>
class gameOfLife : public std::enable_shared_from_this<gameOfLife<cellDerived>>
{
    int sizeX;
    int sizeY;
    std::vector<std::vector<cellDerived>> rows;
    void init(int x, int y)
    {
        rows.resize(sizeX);
        for (int i = 0; i < sizeX; i++)
        {
            for (int j = 0; j < sizeY; j++)
            {
                cellDerived c(i, j);
                rows.at(i).push_back(cellDerived(i, j));
            }
        }
    }
public:
    std::shared_ptr<gameOfLife<cellDerived>> getptr() {
        return shared_from_this();
    }
    cellDerived &get(int x, int y)
    {
        x %= sizeX;
        y %= sizeY;
        if (x < 0) x += sizeX;
        if (y < 0) y += sizeY;
        return rows.at(x).at(y);
    }
    void step()
    {
        for (std::vector<cellDerived> &row : rows)
        {
            for (cellDerived &c : row)
            {

                    c.calculateNextState(shared_from_this());
            }
        }
        for (std::vector<cellDerived> &row : rows)
        {
            for (cellDerived &c : row)
            {
                c.update();
            }
        }
    }

Aucun commentaire:

Enregistrer un commentaire