[英]error: no matching function for call to / candidates are
我一直在嘗試修復此代碼,但我一直遇到同樣的錯誤。 我不確定如何解決,我希望也許這里有人可能。 編譯器消息是:
調用`Date :: Date(int)'沒有匹配函數
注意候選人是:日期::日期(常數日期&)
注意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);
關於什么是錯的任何想法?
電話就像
Date((11, 15, 2010))
可能是罪魁禍首。 內部括號不是將三個整數傳遞給日期構造函數,而是使其傳遞一個參數,其值為表達式11, 15, 2010
的值。 逗號被解釋為逗號運算符,它計算第一個和第二個操作數,丟棄第一個操作數的值,然后返回第二個操作數。
使用Date(11, 15, 2010)
,你應該沒事。
編輯:請注意,如果您傳遞一個臨時的,您的Employee
構造函數必須通過const
引用(無論如何它應該使用它,因為它只是存儲副本而不是以任何方式修改Date
對象)。
逗號運算符計算其第一個參數,拋出結果,並返回第二個參數的結果。
像這樣:
int x = 1,2,3;
之后, x
是3。
你在寫
Date((11, 32, 2114))
其中,因為你添加了幾個括號,所以Date
的參數( 不是參數)是(11, 32,2114)
,這是一個int
- 2114
- 因為你使用的是逗號運算符。
將其更改為
Date(11, 32, 2114)
但錯誤消息與您說它的行不對應。
您從這些錯誤消息中獲取的錯誤消息是“無法將非const引用綁定到臨時”,這是因為構造函數的Date
參數是非const引用。
要么使它們成為const引用,要始終如一地執行:
Employee(const string& fName, const string& lName, int id,
const Date &bday, const Date &hday, double bpay);
或者只是忘記傳遞引用(這是一種被高估的做法):
Employee(string fName, string lName, int id,
Date bday, Date hday, double bpay);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.