1

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

4
  • It's a slightly misleading message – the problem is that you're passing const permutation_t & where the constructor expects permutation_t *. You need to spend some more time thinking about constness, ownership, and lifetime. Commented Dec 27, 2022 at 13:15
  • On a side note, your seemingly random permutation of the arguments between add_player and Player's constructor makes it more likely that you're going to make an error somewhere. Be systematic and consistent. Commented Dec 27, 2022 at 13:21
  • All questions here should have all relevant information in the question itself as plain text. Links can stop working at any time making questions meaningless. Code, data, or errors shown as images cannot be copy/pasted; or edited or compiled for further research and investigation. Can you edit this question, removing and replacing all links and images with all relevant information as plain text? All code must meet all requirements for a minimal reproducible example. You'll find many other questions here, with a minimal reproducible example, in plain text. Please use them as an example for how your question should look. Commented Dec 27, 2022 at 13:26
  • "why it recognize int& and not just int" -- because that is how you are calling the constructor. For the first parameter, you specified playerId, which is the name of an int. The type of the name of an int is int& (which can convert to int as needed). This is normal -- the compiler is avoiding assumptions. Focus on the types where the difference is more than just an & (i.e. don't worry about X& vs. X; focus on const X& vs. X*, for some type X). Commented Dec 27, 2022 at 13:38

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks , is it possible to convert the reference to a pointer and then assaign it to permutation_t* ?
@Elad Yes, you can use the & operator like &spirit to pass it as argument. But note then you'll have to take care of the const.
@Elad While it is possible to convert the two pointer members to references there is a side effect that you may well not expect. The copy constructor is automatically deleted. This can make things like putting, and manipulating your class objects in a vector rather messy. You can fix this by writing your own copy constructor as of c++20. Search for std::construct_at

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.