C++: No matching function to call to const char

codinglikejesus picture codinglikejesus · Jan 19, 2018 · Viewed 8.7k times · Source

im trying to create a object called player in class player.

#include <string>
#ifndef PLAYER_HH
#define PLAYER_HH

using namespace std;

class Player
{
public:
    Player(string name, int points);
    const string get_name();
    int get_points();
    void add_points(int pts);
    bool has_won();
private:
    string _name;
};

#endif // PLAYER_HH

from player.cpp:

#include <string>

using namespace std;

Player::Player(string name):
    _name(name), _points(0){
}

Now, the problem is, in the main function i get this error:

 error: no matching function for call to ‘Player::Player(const char [6])’
 Player player1 = Player("Matti");
                                ^

Shouldn't the compiler be able to convert it to a string?

edit: Here is the full main.cpp that i'm not supposed to be changing:

#include <cstdlib>
#include <iostream>
#include <string>
#include "player.hh"


int main()
{
    Player player1 = Player("Matti");
    Player player2 = Player("Teppo");
    Player* in_turn = 0;

    int turn = 1;
    while (true)
    {
        if (turn % 2 != 0)
        {
            in_turn = &player1;
        }
        else
        {
            in_turn = &player2;
        }

        std::cout << "Enter the score of player " << in_turn->get_name()
             << " of turn " << turn << ": ";
        int pts = 0;
        std::cin >> pts;

        in_turn->add_points(pts);
        if (in_turn->has_won())
        {
            std::cout << "Game over! The winner is " << in_turn->get_name() << "!" << std::endl;
            return EXIT_SUCCESS;
        }

        std::cout << std::endl;
        std::cout << "Scoreboard after turn " << turn << ":" << std::endl;
        std::cout << player1.get_name() << ": " << player1.get_points() << "p" << std::endl;
        std::cout << player2.get_name() << ": " << player2.get_points() << "p" << std::endl;
        std::cout << std::endl;

        turn += 1;
    }

    return EXIT_SUCCESS;
}

You guys are awesome with your fast answers :-)

Answer

Detonar picture Detonar · Jan 19, 2018

You declared the constructor of player as Player(string name, int points);.

If you define a function with two parameters you have to use both.

Create your object with

Player player1 = Player("Matti", 0);

If you still want to call it with just one parameter you have to set a default value like this.

class Player
{
public:
    ...
    Player(string name, int points = 0); // replace 0 with whatever you want to be default.
    ...
}

Then you can use both variants. The one above and the one you attempted

Player player1 = Player("Matti");

Of course the function header of your definition has to match the one in the declaration:

Player::Player(string name, int points):
    _name(name), _points(points){
}

It's important not to write the default value inside dhe definition because this will most likely produce an compiler error.