找不到匹配项

Could not find a match

本文关键字:找不到      更新时间:2023-10-16

我得到以下错误,我不知道为什么:

Could not find a match for 'PEmployee::PEmployee(char *, double)' in function main()

这是我的代码:

class PEmployee
{
    public:
        PEmployee();
        PEmployee(string employee_name, double initial_salary);
        void set_salary(double new_salary);
        double get_salary() const;
        string get_name() const;
    private:
        Person person_data;
        double salary;
};
int main()
{
    PEmployee f("Patrick", 1000.00);
    cout << f.get_name() << " earns a salary of ";
    << f.get_salary() << endl;
    return 0;
}

有人能告诉我为什么会出现这个错误吗?

谢谢。

类型为char *std::string的构造函数不是显式的,所以我不知道为什么会出现这个错误。编译器应该认识到它可以为您动态创建std::string

发件人http://en.cppreference.com/w/cpp/string/basic_string/basic_string:

basic_ string(const CharT*s,const分配器&alloc=Allocator());