Почему умный указатель вызывает std :: length_error - PullRequest
0 голосов
/ 26 марта 2020

Я использую умный указатель в моем коде. Когда я пытаюсь скомпилировать мою программу, Visual Studio выскакивает это уведомление в классе vector:

Необработанное исключение в расположении 0x00007FFD725A9179 в игре life.exe: исключение Microsoft C ++: std :: length_error в ячейке памяти 0x00000004A491E9B0.

Вот мой код:

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();
            }
        }
    }

это экран от отладчика введите описание изображения здесь

...