1

I am having an issue with overloading the assignment operator. I am getting a "terminator called recursively" error in the console.

I am relatively new to data structures and I'm having trouble debugging this issue.

Here is the class declaration:

class Player
{
public:
    Player(const unsigned int x_, const unsigned int y_, const char i_);
    Player(const Player& p_);
    ~Player();

    unsigned int getX() const;
    unsigned int getY() const;
    char getI() const;

    void updatePosition(int newX, int newY);

    Player& operator=(const Player& p_);
private:
    struct Position{
        unsigned int x;
        unsigned int y;
    };

    Position* pos;

    char i;
};

And here is the implementation of the operator= function:

Player& Player::operator=(const Player& p_)
{
    delete pos;
    pos = nullptr;

    pos = new Position();
    pos->x = p_.getX();
    pos->y = p_.getY();

    i = p_.getI();

    return *this;
}
Alex Csillag
  • 111
  • 1
  • 1
  • 6

1 Answers1

1

I think that using Position* pos is not a good choice. You always have to initialize it by constructor.

Although you did so, in Player& operator=(const Player& p_), i think there is trouble with first line and second line. You already removed pos in memory by delete pos but you're trying to set pos to nullptr.

Hun
  • 26
  • 5