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

12.5k Views Asked by At

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 :-)

3

There are 3 best solutions below

3
Detonar On BEST ANSWER

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.

0
helb On

The conversion from const char[6] to std::string will work and is not the issue here.

Your constructor has two parameters, you cannot simply omit the second one. If you like to create Player objects with a default score, declare your constructor as:

Player(string name, int points = 0);
0
Mateusz Grzejek On

First of all, you declare your constructor having two parameters:

Player(string name, int points);

but define it as having only one:

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

That should give you compilation error. Either remove the second param from the declaration in class body and keep main.cpp as it is or add second param to the definition in player.cpp:

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

and then specify value for 'points' explicitly:

Player("Matti", 0);

You can also have both - just add default value for points:

class Player
{
...
   Player(string name, int points = 0);
};

Then, both of these lines will work:

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