so I've created a code with STL list which works just fine
Here is the code
// here is main.h file
#include <iostream>
#include <iomanip>
#include <list>
using namespace std;
class Person
{
public:
//Person();
//~Person();
int getPosition()
{
return position;
};
void setPosition(int n)
{
position = n;
};
private:
int position;
};
//here is main.cpp file
#include "main.h"
int main ()
{
list <Person> circ;
Person player;
int i;
for (i = 0; i < 4; i++) // insert players into the circle
{
player.setPosition(i);
circ.push_back(player);
}
int remove = 0;
list<Person>::iterator j = circ.begin();
while (j!= circ.end())
{
if (j->getPosition() == remove) // if the player has the target
// position
{
j = circ.erase(j); // eliminate player from the circle
}
j++;
}
My problem is when I set the variable "remove" to be = 3 which is I think the end of the list, the program crashes directly, but when I assign it from 0 - 2 it works just fine. Can anyone help me fixing this, please?