Hi,
I am writing a couples class, one base, and one derived to track an employees pay records. It has a static private ptr array to store Employee and EmployeePay objects.
static Employee* empArray[];
Employee has first and last name, ssn, id number and a date. EmployeePay derives from Employee. It has three more fields. I wrote/in the process of writing a static function that would open a file, create a new object of EmployeePay, then read in a line using virtual methods to get the base class fields filled, the fill the derived(EmployePay) objects fields.
The problem is that the compiler doesn't like me trying to declare memory for a new EmployeePay object and store it the Employee* array[]. I declared this static function read() in the Employee class. But when I declare the new object,
empArray[numEmployees] = new EmployeePay;
I get an error in VS2003 "error C2061: syntax error : identifier EmployeePay".
I am trying to use polymorphism to store both the base object and the derived object in the static Employee *array[]. Then later, another static function will traverse the array and tell each object to print itself. I am trying to declare an EmployeePay object into the array so I can use both the base and derived fields to store data.
Am I going about this wrong? Should I declare the static read() function in the derived class? Any help/suggestions would be appreciated.
//// file Employee.h
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include "Date.h"
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Employee
{
// overload the insertion and extraction operators
friend ostream& operator<<(ostream&, const Employee&);
friend istream& operator>>(istream&, Employee&);
protected:
bool isValid;
enum { SSNSIZE = 12, IDSIZE = 6, MAXARRAY = 10};
Date hireDate;
char ssn[SSNSIZE];
char id[IDSIZE];
string firstName;
string lastName;
static int numEmployees; // tracks number of employees
static Employee* empArray[]; // array of pointers to Employee type
void validateID();
void validateSSN();
public:
void GetFileData(ifstream& file);
void PrintData();
static void Display();
static void ReadFile();
}; //end class Employee
#endif
//// file Employee.cpp
#include "Employee.h"
using namespace std;
//** Overloaded operators
// overload istream
istream& operator>>(istream& isObject, Employee& newObject)
{
isObject >> newObject.firstName >> newObject.lastName >> newObject.ssn
>> newObject.id >> newObject.hireDate;
return isObject;
}
// overload ostream
ostream& operator<<(ostream& osObject, const Employee& newObject)
{
osObject << newObject.firstName << newObject.lastName << newObject.ssn
<< newObject.id << newObject.hireDate;
return osObject;
}
// Static variables
int Employee::numEmployees;
Employee* Employee::empArray[MAXARRAY];
// Static Function to display all records
void Employee::Display()
{
//for (int i=0; i < numEmployees; i++)
// empArray[numEmployees]->
}
// Static Function to get data from file
void Employee::ReadFile()
{
numEmployees=0; // number of employees in the array
ifstream inFile; // declare file stream
char fileName[13]; // char[] to store file name from user
cin.get(fileName, 13); // get file name from user
inFile.open(fileName); // open file
// **** Getting an error from VS2003 "error C2061: syntax error : identifier EmployeePay
// When i use an Employee object it compiles and runs correctly
empArray[numEmployees] = new EmployeePay;
empArray[numEmployees]->GetFileData(inFile);
cout << *empArray[numEmployees];
inFile.close();
empArray[numEmployees]->PrintData();
}
void Employee::validateID()
{
}
void Employee::validateSSN()
{
}
void Employee::GetFileData(ifstream& file)
{
file >> *this;
}
void Employee::PrintData()
{
cout << *this;
//// file EmployeePay.h
#ifndef EMPLOYEEPAY_H
#define EMPLOYEEPAY_H
#include "Employee.h"
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class EmployeePay: public Employee
{
// overload the insertion and extraction operators
friend ostream& operator<<(ostream&, const EmployeePay&);
friend istream& operator>>(istream&, EmployeePay&);
private:
double annualPay;
double monthlyPay;
unsigned int dependents;
void validateDependents();
void validateAnnualPay();
public:
void GetFileData(ifstream& file);
void PrintData();
// Overloaded operators
//const EmployeePay& operator =(const EmployeePay&);
}; //end class EmployeePay
#endif
Note: the class is not fully complete/implemented yet, just having problems with read(). I did'nt include the EmployeePay.cpp because it is insignificant at this point.
Cheers :cool: