gpt4 book ai didi

c++ - 错误 : no matching function for call to/candidates are

转载 作者:行者123 更新时间:2023-11-28 02:49:36 26 4
gpt4 key购买 nike

我一直在尝试修复这段代码,但我总是遇到同样的错误。我不确定如何修复,我希望这里有人可以解决。编译器消息是:

no matching function for call to `Date::Date(int)'

note candidates are: Date::Date(const Date&)

note Date::Date(int, int, int)

note Date::Date()

这是主要文件:

/*-------------------- Lab7.cpp ------------------*/
#include <iostream>
#include <iomanip>
#include "Employee.h"
using namespace std;

int main ( )
{
Employee emp1;
emp1.printPInfo( );

Employee emp2("John", "Doe", 111111, Date((10,25,1990)),
Date((11, 15, 2010)), 750.00);
emp2.printPInfo( );

Employee emp3;
emp3.readPInfo( );
emp3.printPInfo( );

Employee emp4("Peter", "Parker", 222222, Date((8, 12, 1985)),
Date((11, 32, 2114)), 800.00);
return 0;
}

这是包含在主文件中的 Employee.h 文件:

/*-------------------- Employee.h ---------------*/
#include <string>
#include "Date.h"
using std::string;

class Employee
{
public:
Employee( );
Employee(string fName, string lName, int id,
Date &bday, Date &hday, double bpay);
void readPInfo( );
void readPayInfo( );
void printPInfo( );
double getBpay( );
double getGpay( );
double computeTax( );
void printPayInfo( );
private:
string firstName, lastName;
int idNum;
Date birthDay;
Date dateHired;
double basePay;
};

最后是 Employee.h 中包含的 Date.h 文件

/*-------------------- Date.h --------------------*/
#ifndef DATE_H
#define DATE_H
class Date
{
public:
Date(int m = 1, int d = 1, int y = 1);
void inputDate( );
void outputDate( );
int getMonth( ), getDay( ), getYear( );
private:
int month, day, year;
void checkDate( );
};
#endif

最后,这里是上面头文件的两个cpp文件:

/*------------------- Date.cpp -------------------------*/
#include <iostream>
#include <cstdlib>
#include "Date.h"
using namespace std;

Date:: Date( int m, int d, int y)
{
month = m;
day = d;
year = y;
checkDate( );
}

void Date:: checkDate( )
{
int m, d, y;
m = getMonth( );
d = getDay( );
y = getYear( );

static int daysofmonth[12]= {31, 28, 30, 31,
30, 31, 30, 31,
30, 31, 30, 31};

if ( m < 1 || m >12)
cout << "\nInvalid Month.";
exit(1);
if (d < 1 || d > daysofmonth[m - 1])
cout << "\nInvalid Day.";
exit(2);
if (y < 1960 || y > 2013)
cout << "\nInvalid Year.";
exit(3);
}

void Date:: inputDate( )
{
cin >> month >> day >> year;
checkDate( );
}

void Date:: outputDate( )
{
cout << getMonth( ) << "/"
<< getDay( ) << "/" << getYear( );
}

int Date:: getMonth( )
{
return (month);
}

int Date:: getDay( )
{
return (day);
}
int Date:: getYear( )
{
return (year);
}

/*------------------ Employee.cpp ---------------*/
#include <iostream>
#include <iomanip>
#include "Employee.h"
using namespace std;

/*----------------- constructors ----------------*/
Employee ::Employee( ): idNum(999999),
dateHired(Date(1, 1, 2013))
{
basePay = 0.00;
}

Employee ::Employee(string fname, string lname,
int id, Date &bday, Date &hday, double bpay)
{
firstName = fname;
lastName = lname;
idNum = id;
birthDay = bday;
dateHired = hday;
basePay = bpay;
}
/*-------------- Member Functions ---------------*/

void Employee :: readPInfo( )
{
cin >> firstName >> lastName >> idNum;
birthDay.inputDate( );
dateHired.inputDate( );
}

void Employee :: readPayInfo( )
{
cin >> basePay;
}
void Employee :: printPInfo( )
{
cout << setw(10) << "Name:\t" << lastName
<< ", " << firstName << endl;
cout << setw(10) << "ID:\t" << idNum << endl;
cout << setw(10) << "DOB:\t";
birthDay.outputDate( );
cout << endl << setw(10) << "BOH:\t";
dateHired.outputDate( );
cout << endl;
}

double Employee :: getBpay( )
{
return(basePay);
}

double Employee :: computeTax( )
{
double taxDeduct, gross;
gross = getGpay( );

if(gross > 1000) taxDeduct = gross * .20;
else if(gross >= 800) taxDeduct = gross * .18;
else if(gross >= 600) taxDeduct = gross * .15;
else taxDeduct = gross *.10;

return(taxDeduct);
}

void Employee :: printPayInfo( )
{
double gross, tax;
gross = getGpay( );
tax = computeTax( );
cout << "\nTax Deduction:\t$" << tax;
cout << "\nNetPay:\t$" << gross - tax;
}

错误指向两行:

Employee emp2("John", "Doe", 111111, (Date(10,25,1990)),(Date(11, 15, 2010)), 750.00);
Employee emp4("Peter", "Parker", 222222, (Date(8, 12, 1985)),(Date(11, 32, 2114)), 800.00);

有什么问题吗?

最佳答案

喜欢的电话

Date((11, 15, 2010)) 

可能是罪魁祸首。内部括号使它传递一个参数,而不是将三个整数传递给日期构造函数,该参数的值是表达式 11, 15, 2010 的值。逗号被解释为逗号运算符,它计算第一个和第二个操作数,丢弃第一个的值,并返回第二个。

使用 Date(11, 15, 2010) 应该没问题。

编辑:请注意,如果您要传递一个临时变量,您的 Employee 构造函数必须通过 const 引用来获取它们(无论如何它应该是因为它只是存储一个拷贝而不是以任何方式修改 Date 对象)。

关于c++ - 错误 : no matching function for call to/candidates are,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23356561/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com