C++不存在从"Date"到"Date (*)()"的合适转换函数

C++ no suitable conversion function from "Date" to "Date (*)()" exists

本文关键字:Date 转换 函数 不存在 C++      更新时间:2023-10-16

我正在处理一个工资单程序,但我一直在犯一个错误:

严重性代码描述项目文件行禁止显示状态错误(活动)不存在从"日期"到"日期(*)()"的合适转换函数Payroll c:\Users\Bart\Documents\Visual Studio 2015\Projects\Payroll\Payroll.cpp 58

这是我的主菜:

int main()
{
int count = 0;
int option = 0;
int month;
int day;
int year;
cout << fixed << setprecision(2);
Employee *employees[arraySize];
cout << "Welcome to the payroll program!" << endl;
cout << "Please enter the current month: ";
cin >> month;
cout << "Please enter the current day: ";
cin >> day;
cout << "Please enter the current month: ";
cin >> year;
Date currentDate(month, day, year);
while (option != 3)
{
    cout << endl << "Please select one of the following options" << endl;
    cout << "1 - Enter new employee information" << endl;
    cout << "2 - View payroll" << endl;
    cout << "3 - Exit the application" << endl;
    cout << "Please enter your option: ";
    cin >> option;
    switch (option)
    {
        case 1:
            if (count < arraySize)
                {
                    count+= createEmployee(employees, count, currentDate);
                    //This is where the error is occurring, currentDate is underlined
                }
            else
            {
                cout << "You cannot enter any more than " << arraySize << " employees." << endl;
            }
                break;
        case 2:
            displayEmployees(employees, count);
            break;
        case 3:
            break;
        default:
            cout << "Please enter a valid menu selection";
    }
}
return 0;
}

这里是我的createEmployee方法的第一部分:

int createEmployee(Employee *employees[], int index, Date date())
{
string firstName;
string middleName;
string lastName;
int bDay;
int bMonth;
int bYear;
Date currentDate = date();
string SSN;
int option;

正如Eissa N.所说,编译器认为您正试图将类型为Date的对象作为函数指针Date(*)()传递。

createEmployee函数定义(和声明)中的Date date()中删除()

另一种解决方案是修复createEmployee定义以接受Date (*date)(month, day, year)函数指针,并在createEmployee函数中将函数createDate传递给它,并将参数传递给它。(您必须将createDate定义为一个函数)