0

我很抱歉问这个问题,因为它可能在这里的某个地方得到了回答,但到目前为止我的搜索一直没有结果。

如果我使用我的参数化构造函数,我可以将我的类对象传递给我的输出函数,一切都很好。如果我使用默认构造函数,它会失败:

1>c:\<path>\project_04.cpp(152): error C2664: 'printCheck' : cannot convert parameter 1 from 'AdamsEmployee (__cdecl *)(void)' to 'AdamsEmployee'
1>          No constructor could take the source type, or constructor overload resolution was ambiguous

当我尝试将它传递给输出函数时,我的对象的鼠标悬停表示:

Error: No suitable constructor exists to convert from "AdamsEmployee ()" to "AdamsEmployee"

这是我的默认构造函数:

AdamsEmployee::AdamsEmployee()
{
    AdamsEmployee::employeeNumber = -1;
    AdamsEmployee::employeeName = "";
    AdamsEmployee::employeeAddress = "";
    AdamsEmployee::employeePhone = "";
    AdamsEmployee::employeeHourlyWage = 0.0;
    AdamsEmployee::employeeHoursWorked = 0.0;
}

这是我的参数化构造函数:

AdamsEmployee::AdamsEmployee(int employeeNumber, string employeeName, string
employeeAddress, string employeePhone, double employeeHourlyWage,
doubleemployeeHoursWorked )
{
    AdamsEmployee::employeeNumber = employeeNumber;
    AdamsEmployee::employeeName = employeeName;
    AdamsEmployee::employeeAddress = employeeAddress;
    AdamsEmployee::employeePhone = employeePhone;
    AdamsEmployee::employeeHourlyWage = employeeHourlyWage;
    AdamsEmployee::employeeHoursWorked = employeeHoursWorked;
}

调用输出的行:

printCheck( emp1 );

输出功能:

void printCheck( AdamsEmployee employee )
{
// Display the mock paycheck.
cout << "----------------------------------H&H Systems----------------------------------" << endl;
cout << "\nPay to the order of " << employee.getName() << ".....$" << employee.calcPay() << endl;
// Display the simulated paystub.
cout << "\nGoliath National Bank" << endl;
cout << "-------------------------------------------------------------------------------" << endl;
cout << "Hours worked: " << employee.getHoursWorked() << endl;
cout << "Hourly wage: " << employee.getWage() << endl;
} // End printCheck()

如果我添加参数,一切正常。搜索返回许多似乎并不适用的情况。您需要更多信息吗?

我究竟做错了什么?

编辑:感谢所有帮助!

4

2 回答 2

2

您的错误表明您正在传递一个函数,就好像您已声明AdamsEmployee emp1(). 正如一条评论所提到的,这可能是由于解析不明确。这很常见,有一个完整的 stackoverflow 标签:https ://stackoverflow.com/questions/tagged/most-vexing-parse

于 2013-09-22T05:20:16.703 回答
0

您可能在默认构造函数的类中缺少构造函数的声明。

于 2013-09-22T05:30:20.863 回答