我收到以下错误,我无法弄清楚原因:
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 Allocator& alloc = Allocator() );
关于c++ - 找不到匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12256243/