Home > Net >  Using reference and pointers in constructor
Using reference and pointers in constructor

Time:12-28

I have a problem with my constructor , can't figure out why the constructor is not recognized , and why it recognize int& and not just int ?

I try to change spirit and multispirit member to reference instead of pointer but it doesn't work , any help is welcome , thanks !

class Player{
    public:
    int playerID;
    int teamPlayerID;
    int playerGamesPlayed;
    int ability;
    int cards;
    bool goalkeeper;
    const permutation_t* spirit;
    const permutation_t* multSpirit;
    Team* teamOfPlayer;

    public:
    Player(int playerID,int cards,int playerGamesPlayed,bool goalkeeper, permutation_t* spirit, permutation_t* multSpirit,int teamID,int ability):playerID(playerID),cards(cards),playerGamesPlayed(playerGamesPlayed),goalkeeper(goalkeeper),spirit(spirit),multSpirit(multSpirit),teamPlayerID(teamID),ability(ability){
      teamOfPlayer = NULL;
     }

The function :

StatusType world_cup_t::add_player(int playerId, int teamId,
                                           const permutation_t &spirit, int gamesPlayed,
                                           int ability, int cards, bool goalKeeper)
        {
            Player* addedPlayer = new Player(playerId,cards,gamesPlayed,goalKeeper,spirit,spirit,teamId,ability);
        }

The error : enter image description here

CodePudding user response:

The problem is that the fourth parameter spirit of your constructor is of type permutation_t* and so it expects a pointer to a permutation_t but the type of the argument spirit as declared in the parameter of the member function world_cup_t::add_player is const Permutation_t&.

That is, there is a mismatch in the types of the fourth parameter of the constructor and the corresponding argument that you're passing to it.

To solve this error, we have to pass an argument that is either of the same type or convertible to parameter type.

  • Related