No Default Constructor
May 8, 2013 at 2:29pm UTC
I am getting an error on line 13 of the .cpp saying there is no default constructor. Any help would be appreciated.
Employee.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
using namespace std;
class Employee
{
private :
string employeeName;
int employeeNumber;
string hireDate;
public :
Employee (string eName, int eNumber, string hDate)
{
employeeName = eName;
employeeNumber = eNumber;
hireDate = hDate;
}
void setEmployeeName (string eName)
{
employeeName = eName;
}
void setEmployeeNumber (int eNumber)
{
employeeNumber = eNumber;
}
void setHireDate (string hDate)
{
hireDate = hDate;
}
string getEmployeeName()
{
return employeeName;
}
int getEmployeeNumber()
{
return employeeNumber;
}
string getHireDate()
{
return hireDate;
}
};
#endif
ShiftSupervisor.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#ifndef SHIFTSUPERVISOR_H
#define SHIFTSUPERVISOR_H
#include "Employee.h"
#include <string>
class ShiftSupervisor : public Employee
{
private :
int salary;
int bonus;
public :
ShiftSupervisor (string eName, int eNumber, string hDate,
int empSalary, int empBonus) : Employee(eName, eNumber, hDate)
{
salary = empSalary;
bonus = empBonus;
}
void setSalary(int empSalary)
{
salary = empSalary;
}
void setBonus(int empBonus)
{
bonus = empBonus;
}
int getSalary()
{
return salary;
}
int getBonus()
{
return bonus;
}
};
#endif
ShiftSupervisorDemo.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include <iostream>
#include <string>
#include <iomanip>
#include "ShiftSupervisor.h"
using namespace std;
char again;
int main()
{
do
{
ShiftSupervisor shiftSupervisor;
string supervisorName;
int supervisorNumber;
string supervisorHireDate;
int supervisorSalary;
int supervisorBonus;
cout << "Shift Supervisor Class" << endl;
cout << "-----------------------" << endl;
cout << "What is the supervisor's name? " << endl;
cin >> supervisorName;
cout << "What is the supervisor's number? " ;
cin >> supervisorNumber;
cout << "When was the supervisor hired? " ;
cin >> supervisorHireDate;
cout << "What is the supervisor's salary? " ;
cin >> supervisorSalary;
cout << "What is the supervisor's bonus? " ;
cin >> supervisorBonus;
shiftSupervisor.setEmployeeName(supervisorName);
shiftSupervisor.setEmployeeNumber(supervisorNumber);
shiftSupervisor.setHireDate(supervisorHireDate);
shiftSupervisor.setSalary(supervisorSalary);
shiftSupervisor.setBonus(supervisorBonus);
cout << "Employee Name: " << shiftSupervisor.getEmployeeName() << endl;
cout << "Employee Number: " << shiftSupervisor.getEmployeeNumber() << endl;
cout << "Hire Date: " << shiftSupervisor.getHireDate() << endl;
cout << "Salary: $" << shiftSupervisor.getSalary() << endl;
cout << "Bonus: $" << setprecision(2) << fixed << shiftSupervisor.getBonus() << endl;
cout << "Total Earnings: $" << shiftSupervisor.getSalary() + shiftSupervisor.getBonus() << endl;
cout << "Do you want to run this program again? Y/N: " ;
cin >> again;
} while (again == 'y' || again == 'Y' );
return 0;
}
Last edited on May 8, 2013 at 3:22pm UTC
May 8, 2013 at 2:34pm UTC
A "default constructor" is the constructor that takes no arguments.
1 2
ShiftSupervisor shiftSupervisor; // <- this will default construct a ShiftSupervisor
// because you are not using any arguments when calling the constructor
However, the only constructor in the ShiftSupervisor class takes
five arguments. Therefore there is no default constructor that you can call, which is why you get this error.
Solutions are:
1) Use the constructor you wrote, rather than the default constructor (this means you would create your shiftSupervisor object
after you got all the input from the user, and pass all that data to the constructor rather than calling your setters).
or
2) Write a default constructor for your ShiftSupervisor and Employee classes.
Last edited on May 8, 2013 at 2:34pm UTC
May 8, 2013 at 3:01pm UTC
Okay. I added default constructors to my headers. Now when I run the program, it prompts for a name and then terminates. Any idea how to fix this?
Here is the updated code...
Employee.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
using namespace std;
class Employee
{
private :
string employeeName;
int employeeNumber;
string hireDate;
public :
Employee()
{
employeeName = "" ;
employeeNumber = 0;
hireDate = "" ;
}
Employee (string eName, int eNumber, string hDate)
{
employeeName = eName;
employeeNumber = eNumber;
hireDate = hDate;
}
void setEmployeeName (string eName)
{
employeeName = eName;
}
void setEmployeeNumber (int eNumber)
{
employeeNumber = eNumber;
}
void setHireDate (string hDate)
{
hireDate = hDate;
}
string getEmployeeName()
{
return employeeName;
}
int getEmployeeNumber()
{
return employeeNumber;
}
string getHireDate()
{
return hireDate;
}
};
#endif
ShiftSupervisor.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#ifndef SHIFTSUPERVISOR_H
#define SHIFTSUPERVISOR_H
#include "Employee.h"
#include <string>
class ShiftSupervisor : public Employee
{
private :
int salary;
int bonus;
public :
ShiftSupervisor() : Employee()
{
salary = 0;
bonus = 0;
}
ShiftSupervisor (string eName, int eNumber, string hDate,
int empSalary, int empBonus) : Employee(eName, eNumber, hDate)
{
salary = empSalary;
bonus = empBonus;
}
void setSalary(int empSalary)
{
salary = empSalary;
}
void setBonus(int empBonus)
{
bonus = empBonus;
}
int getSalary()
{
return salary;
}
int getBonus()
{
return bonus;
}
};
#endif
ShiftSupervisorDemo.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include <iostream>
#include <string>
#include <iomanip>
#include "ShiftSupervisor.h"
using namespace std;
char again;
int main()
{
do
{
ShiftSupervisor shiftSupervisor;
string supervisorName;
int supervisorNumber;
string supervisorHireDate;
int supervisorSalary;
int supervisorBonus;
cout << "Shift Supervisor Class" << endl;
cout << "-----------------------" << endl;
cout << "What is the supervisor's name? " << endl;
cin >> supervisorName;
cout << "What is the supervisor's number? " ;
cin >> supervisorNumber;
cout << "When was the supervisor hired? " ;
cin >> supervisorHireDate;
cout << "What is the supervisor's salary? " ;
cin >> supervisorSalary;
cout << "What is the supervisor's bonus? " ;
cin >> supervisorBonus;
shiftSupervisor.setEmployeeName(supervisorName);
shiftSupervisor.setEmployeeNumber(supervisorNumber);
shiftSupervisor.setHireDate(supervisorHireDate);
shiftSupervisor.setSalary(supervisorSalary);
shiftSupervisor.setBonus(supervisorBonus);
cout << "Employee Name: " << shiftSupervisor.getEmployeeName() << endl;
cout << "Employee Number: " << shiftSupervisor.getEmployeeNumber() << endl;
cout << "Hire Date: " << shiftSupervisor.getHireDate() << endl;
cout << "Salary: $" << shiftSupervisor.getSalary() << endl;
cout << "Bonus: $" << setprecision(2) << fixed << shiftSupervisor.getBonus() << endl;
cout << "Total Earnings: $" << shiftSupervisor.getSalary() + shiftSupervisor.getBonus() << endl;
cout << "Do you want to run this program again? Y/N: " ;
cin >> again;
} while (again == 'y' || again == 'Y' );
return 0;
}
Topic archived. No new replies allowed.