I am using a smart pointer in my code. When I try compile my program,
Visual Studio pops up this notification in the vector
class:
Unhandled exception at location 0x00007FFD725A9179 in game of life.exe: Microsoft C ++ exception: std :: length_error at memory location 0x00000004A491E9B0.
Here is my code:
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();
}
}
}
this is screen from debugger enter image description here