Hello, I have an assignment that generally deals with Inherited classes and I'm stuck at a problem where I'm getting an error when I try calling the constructor:
error: cannot call constructor ‘HourlyEmployee::HourlyEmployee’ directly [-fpermissive]
employee = HourlyEmployee::HourlyEmployee(sfirstname, slastname, semployeeId, srate, shours);
This is my parser class where I get the error. The comments are what I tried already:
Code:
#include <string>
#include "StaffMember.h"
#include "HourlyEmployee.h"
using namespace std;
#ifndef STAFFMEMBERPARSER_H
#define STAFFMEMBERPARSER_H
class StaffMemberParser
{
public:
static StaffMember* parseStringToMember(string lineToParse)
{
string stype, sfirstname, slastname, semployeeId, srate, shours, sbonus; //I initialize the data here
int pos, pos2, pos3, pos4, pos5, pos6; // because it would give me an error otherwise.
pos = lineToParse.find("/");
stype = lineToParse.substr(0,pos);
pos2 = lineToParse.find("/", pos+1);
sfirstname = lineToParse.substr(pos+1,pos2-pos-1);
pos3 = lineToParse.find("/", pos2+1);
slastname = lineToParse.substr(pos2+1,pos3-pos2-1);
pos4 = lineToParse.find("/", pos3+1);
semployeeId = lineToParse.substr(pos3+1,pos4-pos3-1);
if(stype == "Volunteer")
{
// Volunteer:Volunteer(sfirstname,slastname, semployeeId);
}
if(stype == "FullTimeEmployee")
{
pos5 = lineToParse.find("/", pos4+1);
srate = lineToParse.substr(pos4+1,pos5-pos4-1);
pos6 = lineToParse.find("/", pos5+1);
sbonus = lineToParse.substr(pos5+1,pos6-pos5-1);
}
if(stype == "HourlyEmployee")
{
pos5 = lineToParse.find("/", pos4+1);
srate = lineToParse.substr(pos4+1,pos5-pos4-1);
pos6 = lineToParse.find("/", pos5+1);
shours = lineToParse.substr(pos5+1,pos6-pos5-1);
// HourlyEmployee::HourlyEmployee(sfirstname, slastname, semployeeId, srate, shours);
StaffMember* employee;
// employee = &(HourlyEmployee(sfirstname, slastname, semployeeId, srate, shours));
employee = HourlyEmployee::HourlyEmployee(sfirstname, slastname, semployeeId, srate, shours); //happens at this line
//return employee;
}
}
};
#endif
Here is the hourlyemployee class:
Code:
// Child class
#include "StaffMember.h" //include the parent class header
using namespace std;
#ifndef HOURLYEMPLOYEE_H
#define HOURLYEMPLOYEE_H
class HourlyEmployee : public StaffMember
{
private:
double rate;
int hoursWorked;
public:
HourlyEmployee(string firstName, string lastName, string memberId, double newRate, int newHoursWorked) : StaffMember(firstName, lastName, memberId)
{
rate = newRate;
hoursWorked = newHoursWorked;
}
void computePay()
{
pay = rate*hoursWorked;
}
//overriding the printInfo function from the parent class
virtual void printInfo()
{
cout << "\nHourly Employee:";
StaffMember:printInfo();
cout << "Rate:\t\t\t$" << rate << endl;
cout << "Hours:\t\t\t$" << hoursWorked << endl;
}
};
#endif
Staffmember class:
Code:
#include <string>
using namespace std;
#ifndef STAFFMEMBER_H
#define STAFFMEMBER_H
class StaffMember
{
protected:
string firstName;
string lastName;
string memberID;
double pay = 0.0;
public:
StaffMember(string newfirstName, string newlastName , string newmemberID)
{
firstName = newfirstName;
lastName = newlastName;
memberID = newmemberID;
pay = 0.0;
}
string getMemberId()
{
return memberID;
}
virtual void computePay() = 0;
virtual void printInfo()
{
cout << "\nFirst name:\t\t" << firstName << endl;
cout << "Last name:\t\t" << lastName << endl;
cout << "Member ID:\t\t" << memberID << endl;
cout << "Pay:\t\t\t$" << pay << endl;
}
};
#endif
Main class:
Code:
// Assignment #: 7
// Name:
// EmailAddress:
// Description: It displays a menu of choices
// (add volunteers, full time employees, or hourly employees,
// compute their pay, search a member, list members,
// quit, display menu) to a user.
// Then it performs the chosen task. It will keep asking a user to
// enter the next choice until the choice of 'Q' (Quit) is
// entered.
#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
#include "StaffMember.h"
#include "StaffMemberParser.h"
using namespace std;
void printMenu();
int main()
{
char input1;
string inputInfo;
bool operation;
vector<StaffMember *> memberList;
printMenu(); // print out menu
do
{
cout << "What action would you like to perform?" << endl;
cin >> input1;
switch (input1)
{
case 'A': //Add Member
cout << "Please enter a member information to add:\n";
cin >> inputInfo;
/***********************************************************************************
ADD your code here to create a pointer to an object of one of child classes of
StaffMember class and add it to the memberList
***********************************************************************************/
StaffMemberParser::parseStringToMember(inputInfo);
break;
case 'C': //Compute Pay
/***********************************************************************************
*** ADD your code here to compute the pay for all members the memberList.
***********************************************************************************/
for (int j=0; j < memberList.size(); j++)
{
memberList.at(j)->computePay();
}
cout << "pay computed\n";
break;
case 'D': //Search for Member
cout << "Please enter a memberID to search:\n";
cin >> inputInfo;
operation = false;
/***********************************************************************************
*** ADD your code here to search a given memberID. If found, set "found" true,
*** and set "found" false otherwise.
***********************************************************************************/
for (int j=0; j < memberList.size(); j++)
{
if (inputInfo == memberList.at(j)->getMemberId())
{
operation = true;
}
else
{
operation = false;
}
}
if (operation == true)
cout << "member found\n";
else
cout << "member not found\n";
break;
case 'L': //List Members
/***********************************************************************************
*** ADD your code here to print out all member objects. If there is no member,
*** print "no member\n"
***********************************************************************************/
if (sizeof(memberList) == 0)
{
cout << "no member\n" << endl;
}
else
{
cout << "there are members" << endl;
for (int j=0; j < memberList.size(); j++)
{
memberList.at(j)->printInfo();
}
}
break;
case 'Q': //Quit
for (int j=0; j < memberList.size(); j++)
{
delete memberList.at(j);
}
break;
case '?': //Display Menu
printMenu();
break;
default:
cout << "Unknown action\n" <<endl;
break;
}
} while (input1 != 'Q' && input1 != 'q'); // stop the loop when Q is read
}
/** The method printMenu displays the menu to a user **/
void printMenu()
{
cout << "Choice\t\tAction\n";
cout << "------\t\t------\n";
cout << "A\t\tAdd Member\n";
cout << "C\t\tCompute Pay\n";
cout << "D\t\tSearch for Member\n";
cout << "L\t\tList Members\n";
cout << "Q\t\tQuit\n";
cout << "?\t\tDisplay Help\n\n";
}