Home > front end >  Constructor cannot be redeclared C
Constructor cannot be redeclared C

Time:04-15

Tp start, I want to say that I am new to C . I am trying to declare my constructor 3 times, because i have 3 more derived classes, which work with different variables. But i get this error, so i don't really know what to do then... The task is to create a class with derived classes of different ways of payments and print it out using an array of polymorphic objects. That's why i use derived classes

class employee{
private:
char type[25];
int hours;
int salary_per_hour;
int days;
int salary_per_day;
int sales_per_month;
int percent_per_sales;
public:
employee() {
    hours = salary_per_hour = days = salary_per_day = sales_per_month = percent_per_sales = 0;
    strcpy(type, "unknown");
}
employee(int h, int d, int sph, char *n) {
    hours = h;
    days = d;
    salary_per_hour = sph;
    strcpy(type, n);
}
employee(int d, int spd, char *n) {
    days = d;
    salary_per_day = spd;
    strcpy(type, n);
}
employee(int b, int spm, int pps, char *n) { //This one doesn't declare
    sales_per_month = spm;
    percent_per_sales = pps;
    strcpy(type, n);
}

int getHours() {
    return hours;
}
int getDays() {
    return days;
}
int getSPD() {
    return salary_per_day;
}
int getSPH() {
    return salary_per_hour;
}
int getSPM() {
    return sales_per_month;
}
int getPPS() {
    return percent_per_sales;
}

char *getType() {
    return type;
}

virtual int salary() {
    cout << "ERROR | salary() must be overridden.\n";
    return 0;
}};
class hourly_payment: public employee{
public:
 hourly_payment(int h, int d, int sph) : employee(h, d, sph, "hourly_payment") { }
 int salary()
 {
     return getHours()*getDays()*getSPH();
 }
};

class daily_payment: public employee{
public:
 daily_payment(int d, int spd) : employee(d, spd, "hourly_payment") { }
 int salary()
 {
    return getDays()*getSPD();
 }
 };

class percent_per_sales_payment: public employee{
public:
 percent_per_sales_payment(int b, int spm, int pps) : employee(b, spm, pps, "percent_per_sales_payment") { }
 int salary()
 {
    return getSPM()*getPPS();
 }
 };

CodePudding user response:

That's because your constructor was redefined. The compiler doesn't care about variable names, all he sees in your case is data type.

employee(int, int, int, char *)

Which is the same in both cases

employee(int b, int spm, int pps, char *n) { //This one doesn't declare
    sales_per_month = spm;
    percent_per_sales = pps;
    strcpy(type, n);
}

and

employee(int h, int d, int sph, char *n) {
    hours = h;
    days = d;
    salary_per_hour = sph;
    strcpy(type, n);
}

Both of them has the same prototype employee(int, int, int, char *)

The easiest solution in your case is to change the order of the parameters.

You can do for example:

employee(char *n, int h, int d, int sph) {
    hours = h;
    days = d;
    salary_per_hour = sph;
    strcpy(type, n);
}

In this case you have a new prototype that doesn't match any other.

employee(char *, int, int, int)

Another way of dealing with this if you have the same type of variables in your constructor is by using enums as a parameter called employee_type and based on which type you pass to employee_type, you can choose what kind of assignment you want to do in your constructor.

CodePudding user response:

Each overload must have a distinct signature; they cannot differ by only the parameter names - those are ignored for the purposes of overload resolution.

You have two declarations with the signature:

employee::employee( int, int, int, char * ) ;
  • Related