0

this is a program about payroll but when compile the program it display error message. getting error while compiling the program dev c++.

  1. [Error] no matching function for call to 'SalariedEmp::SalariedEmp(const char [4], double, double)'

  2. [Note] no known conversion for argument 1 from 'const char [4]' to 'std::string& {aka std::basic_string&}'

i am new in cpp. please some one help me.

    #include <iostream>
    using namespace std;

    class Employee {
        private:
            string name;
            double taxRate;
        public:
            Employee( string&, double );
            string getName();
            virtual double calcSalary() = 0;
    };

    Employee::Employee( string& n, double tr ): name(n){
        taxRate = tr;
    } 

    string Employee::getName() {
        return name;
    }

    class SalariedEmp : public Employee
    {
        private:
            double salary;
        public:
            SalariedEmp(string&,double,double);
            virtual double calcSalary();
    };

    SalariedEmp::SalariedEmp(string& n, double tr, double sal) : Employee( n, tr ) {
        salary = sal;
    }

    double SalariedEmp::calcSalary() {
        double tax = salary * taxRate;
        return salary - tax;
    } 

    class HourlyEmp : public Employee {
        private:
            int hours;
            double hourlyRate;
        public:
            HourlyEmp(string&,double,int,double);
            virtual double calcSalary();
    };

    HourlyEmp ::HourlyEmp( string& n, double tr, int h, double hr ) : Employee( n, tr) {
        hours = h;
        hourlyRate = hr;
    }
    double HourlyEmp::calcSalary()
    {
        double grossPay, tax;
        grossPay = hours * hourlyRate;
        tax = grossPay * taxRate;
        return grossPay - tax;
    } 

    class CommEmp : public Employee
    {
        private:
            double sales;
            double commRate;
        public:
            CommEmp( string&, double, double, double );
            virtual double calcSalary();
    };

    CommEmp::CommEmp( string& n, double tr, double s, double cr ) : Employee(n, tr ) {
        sales = s;
        commRate = cr;
    }

    double CommEmp::calcSalary()
    {
        double grossPay = sales * commRate;
        double tax = grossPay * taxRate;
        return grossPay - tax;
    } 

    int main() {
        Employee* emp[10];
        emp[0] = new SalariedEmp( "Aamir", 0.05, 15000 );
        emp[1] = new HourlyEmp( "Faakhir", 0.06, 160, 50 );
        emp[2] = new CommEmp( "Fuaad", 0.04, 150000, 10 );
        generatePayroll( emp, 10 );
        return 0;
    } 
    void generatePayroll(Employee* emp[], int size) {
        cout << "Name\tNet Salary\n\n";
        for (int i = 0; i < size; i++) {
            cout << emp[i]->getName() << "\t" << emp[i]->calcSalary() << "\n";
        }
    } 
azhar
  • 351
  • 3
  • 13
  • 3
    `string &` --> `const string &` – ChrisMM Jun 21 '21 at 21:47
  • where should implement the const. means with string& – azhar Jun 21 '21 at 21:49
  • 1
    Recommended reading: [When to use virtual destructors?](https://stackoverflow.com/questions/461203/when-to-use-virtual-destructors) – user4581301 Jun 21 '21 at 21:49
  • 1
    This is a bit weird, but `"Aamir"` isn't a `string`. It is a a [string literal](https://en.cppreference.com/w/cpp/language/string_literal), a `const char[6]`. This will implicitly convert to a `string`, but that makes a temporary `string` and because a temporary is often gone before you can use a reference to it, you aren't allowed to bind a reference to a temporary. But you can bind a constant reference to at temporary because by making it `const` you're promising you won't change the contents (and waste whatever time you possibly spent changing the contents). – user4581301 Jun 21 '21 at 21:56
  • 1
    Sorry, I should have specified. That change would be in your constructors. – ChrisMM Jun 21 '21 at 22:00
  • 1
    Extra reading: [Does a const reference class member prolong the life of a temporary?](https://stackoverflow.com/questions/2784262/does-a-const-reference-class-member-prolong-the-life-of-a-temporary) – user4581301 Jun 21 '21 at 22:03
  • thanks a lot to fix the error – azhar Jun 21 '21 at 22:05

0 Answers0