我一直在尝试修复此代码,但我一直遇到同样的错误。我不确定如何解决,我希望也许这里有人可能。编译器消息是:
没有用于调用`Date :: Date(int)'
的匹配函数注意候选人是:Date :: Date(const Date&)
注意Date :: Date(int,int,int)
注意日期::日期()
这是主文件:
/*-------------------- 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;
};
最后是Employ.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);
关于什么是错的任何想法?
答案 0 :(得分:2)
调用
Date((11, 15, 2010))
可能是罪魁祸首。内部括号不是将三个整数传递给日期构造函数,而是传递一个参数,其值为表达式11, 15, 2010
的值。逗号被解释为逗号运算符,它将计算第一个和第二个操作数,丢弃第一个操作数的值,然后返回第二个操作数。
使用Date(11, 15, 2010)
,您应该没问题。
编辑:请注意,如果您要传递一个临时文件,那么您的Employee
构造函数必须通过const
引用它们(无论如何它应该是它,因为它只是存储副本而不是修改Date
以任何方式对象。)
答案 1 :(得分:0)
逗号运算符计算其第一个参数,抛出结果,并返回第二个结果。
像这样:
int x = 1,2,3;
之后,x
为3。
你正在写
Date((11, 32, 2114))
其中,由于您添加了几个括号,Date
的参数(不是参数)为(11, 32,2114)
,这是一个int
- { {1}} - 因为您正在使用逗号运算符。
将其更改为
2114
但是错误消息与您说的行不对应
您从这些中得到的错误消息是&#34;无法将非const引用绑定到临时&#34;以及由于构造函数的 Date(11, 32, 2114)
参数而导致的错误消息,这是非const引用。
要么使它们成为const引用,而是始终如一地执行:
Date
或者忘记传递引用(这是一种被高估的做法):
Employee(const string& fName, const string& lName, int id,
const Date &bday, const Date &hday, double bpay);