user3583833
user3583833

Reputation: 1

error: no matching function for call to / candidates are

I been trying to fix this code but I keep getting the same errors. Im not exactly sure how to fix and I was hoping maybe someone here might be able to. The compiler message is :

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()

Here is the the main file:

/*-------------------- 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;
}

Here is the Employee.h file that is include in the main file:

/*-------------------- 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;
};

And lastly the Date.h file included in Employee.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

Lastly, here are both of the cpp files for the header files above:

/*------------------- 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;
}

The error points two lines which are:

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);

Any ideas as to what is wrong?

Upvotes: 0

Views: 4172

Answers (2)

molbdnilo
molbdnilo

Reputation: 66371

The comma operator evaluates its first arguments, throws the result away, and returns the result of the second.

Like this:

int x = 1,2,3;

after which, x is 3.

You're writing

Date((11, 32, 2114))

where, since you added a couple of brackets, the argument (not arguments) to Date is (11, 32,2114), which is a single int - 2114 - because you're using the comma operator.

Change it to

    Date(11, 32, 2114)

But the error message does not correspond to the lines you say it does.
The error message you get from those is along the lines of "cannot bind a non-const reference to a temporary", and that's because of the constructor's Date parameters, which are non-const references.

Either make them const references, but do it consistently:

Employee(const string& fName, const string& lName, int id,
         const Date &bday, const Date &hday, double bpay);

or just forget about passing references (it's an overrated practice):

Employee(string fName, string lName, int id,
         Date bday, Date hday, double bpay);

Upvotes: 0

T.C.
T.C.

Reputation: 137315

Calls like

Date((11, 15, 2010)) 

is probably the culprit. Instead of passing three ints to the date constructor, the inner parentheses made it pass a single parameter whose value is the value of the expression 11, 15, 2010. The comma gets interpreted as the comma operator, which evaluates the first and second operands, discard the value of the first, and returns the second.

Use Date(11, 15, 2010) and you should be fine.

EDIT: Note that if you are passing a temporary, your Employee constructor must take them by const reference (which it should anyway since it's just storing a copy and not modifying the Date object in any way).

Upvotes: 2

Related Questions