hello I have to implement intelligent pointers, I managed to do something like that but it causes a vector error, I send pointers in the red area and I use the green one. what should I change to prevent a mistake?
gameOfLife.h
enter code here
#pragma once
#include <vector>
#include <string>
template <class cellDerived>
class gameOfLife;
class cell
{
private:
bool alive= false;
protected:
bool nextAlive = false;
std::string aliveSign = "*";
std::string deadSign = ".";
int x;
int y;
public:
cell(int x, int y)
{
this->x = x;
this->y = y;
}
bool isAlive()
{
return alive;
}
void setAlive(bool isAlive)
{
alive = isAlive;
}
template <class cellDerived>
void calculateNextState(gameOfLife<cellDerived> *parent) {};
void update()
{
alive = nextAlive;
return;
}
std::string toString()
{
return alive ? aliveSign : deadSign;
}
};
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();
}
}
}
std::string toString()
{
std::string out;
for (std::vector<cellDerived> &row : rows)
{
for (cellDerived &c : row)
{
out += c.toString() + " ";
}
out += '\n';
}
return out;
}
bool isAlive(int x, int y)
{
return get(x,y).isAlive();
}
void setAlive(int x, int y, bool isAlive)
{
get(x, y).setAlive(isAlive);
}
gameOfLife(int x, int y)
{
init(x, y);
}
~gameOfLife()
{
}
};
now plick when i define main
source.cpp
enter code here
#include <iostream>
#include <fstream>
#include "gameOfLife.h"
#include <cstdlib>
#include <memory>
using namespace std;
class myCell : public cell
{
public:
void calculateNextState(std::shared_ptr<gameOfLife<myCell>> parent)
{
int aliveNeighbours = 0;
for (int i = x - 1; i <= x + 1; i++)
{
for (int j = y - 1; j <= y + 1; j++)
{
if ((x!=i || y!=j) &&parent->isAlive(i, j)) aliveNeighbours++;
}
}
if (this->isAlive())
{
if (aliveNeighbours == 2 || aliveNeighbours == 3)
nextAlive = true;
else nextAlive = false;
}
else if (aliveNeighbours == 3)
{
nextAlive = true;
}
else nextAlive = false;
}
myCell(int x, int y) : cell(x,y)
{
aliveSign = "O";
}
};
int main()
{
fstream file;
file.open("data(GameOFLife).txt", ios::in);
std::shared_ptr<gameOfLife<myCell>> game;
if (file.good() == true) {
int i;
file >> i;
game = std::make_shared<gameOfLife<myCell>>(gameOfLife<myCell>(i,i));
while (!file.eof()) {
file >> i;
int j;
file >> j;
game->setAlive(i, j, true);
}
file.close();
}
while (true)
{
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
std::cout << game->toString();
game->step();
std::cin.ignore();
}
}
this is place when i used smart pointer and when i compile i get vector error
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());
}
Aucun commentaire:
Enregistrer un commentaire