getPlayer()
returns a const Player&
but getIndex()
is a non-const
member function and it is illegal to call a non-const
member function on a const
object. Make getIndex()
const
(as it should be anyway as it is a getter and does not modify the object):
int getIndex() const { return index; }
//^^^^^
It works in the split case:
Player player1=data.getPlayer1();
because of copy of Player
is being made, and player1
is not a const
object and getIndex()
can be invoked.