HELP: class static function - compile errors

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2003
Posts: 8
Reputation: radioman28 is an unknown quantity at this point 
Solved Threads: 0
radioman28 radioman28 is offline Offline
Newbie Poster

HELP: class static function - compile errors

 
0
  #1
Mar 24th, 2005
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.
  1. 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,
  1. 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.
  1. //// file Employee.h
  2. #ifndef EMPLOYEE_H
  3. #define EMPLOYEE_H
  4.  
  5. #include "Date.h"
  6. #include <fstream>
  7. #include <iostream>
  8. #include <iomanip>
  9. #include <string>
  10. using namespace std;
  11.  
  12. class Employee
  13. {
  14. // overload the insertion and extraction operators
  15. friend ostream& operator<<(ostream&, const Employee&);
  16. friend istream& operator>>(istream&, Employee&);
  17.  
  18. protected:
  19. bool isValid;
  20. enum { SSNSIZE = 12, IDSIZE = 6, MAXARRAY = 10};
  21. Date hireDate;
  22. char ssn[SSNSIZE];
  23. char id[IDSIZE];
  24. string firstName;
  25. string lastName;
  26.  
  27. static int numEmployees; // tracks number of employees
  28. static Employee* empArray[]; // array of pointers to Employee type
  29. void validateID();
  30. void validateSSN();
  31. public:
  32. void GetFileData(ifstream& file);
  33. void PrintData();
  34. static void Display();
  35. static void ReadFile();
  36.  
  37. }; //end class Employee
  38. #endif
  39.  
  40. //// file Employee.cpp
  41. #include "Employee.h"
  42. using namespace std;
  43.  
  44. //** Overloaded operators
  45.  
  46. // overload istream
  47. istream& operator>>(istream& isObject, Employee& newObject)
  48. {
  49. isObject >> newObject.firstName >> newObject.lastName >> newObject.ssn
  50. >> newObject.id >> newObject.hireDate;
  51. return isObject;
  52. }
  53.  
  54. // overload ostream
  55. ostream& operator<<(ostream& osObject, const Employee& newObject)
  56. {
  57. osObject << newObject.firstName << newObject.lastName << newObject.ssn
  58. << newObject.id << newObject.hireDate;
  59. return osObject;
  60. }
  61.  
  62. // Static variables
  63. int Employee::numEmployees;
  64. Employee* Employee::empArray[MAXARRAY];
  65.  
  66. // Static Function to display all records
  67. void Employee::Display()
  68. {
  69. //for (int i=0; i < numEmployees; i++)
  70. // empArray[numEmployees]->
  71. }
  72.  
  73. // Static Function to get data from file
  74. void Employee::ReadFile()
  75. {
  76. numEmployees=0; // number of employees in the array
  77. ifstream inFile; // declare file stream
  78. char fileName[13]; // char[] to store file name from user
  79. cin.get(fileName, 13); // get file name from user
  80. inFile.open(fileName); // open file
  81.  
  82.  
  83. // **** Getting an error from VS2003 "error C2061: syntax error : identifier EmployeePay
  84. // When i use an Employee object it compiles and runs correctly
  85. empArray[numEmployees] = new EmployeePay;
  86.  
  87. empArray[numEmployees]->GetFileData(inFile);
  88. cout << *empArray[numEmployees];
  89. inFile.close();
  90. empArray[numEmployees]->PrintData();
  91. }
  92.  
  93. void Employee::validateID()
  94. {
  95. }
  96. void Employee::validateSSN()
  97. {
  98. }
  99.  
  100. void Employee::GetFileData(ifstream& file)
  101. {
  102. file >> *this;
  103. }
  104.  
  105. void Employee::PrintData()
  106. {
  107. cout << *this;
  108.  
  109. //// file EmployeePay.h
  110. #ifndef EMPLOYEEPAY_H
  111. #define EMPLOYEEPAY_H
  112.  
  113. #include "Employee.h"
  114. #include <fstream>
  115. #include <iostream>
  116. #include <iomanip>
  117. #include <string>
  118. using namespace std;
  119.  
  120. class EmployeePay: public Employee
  121. {
  122. // overload the insertion and extraction operators
  123. friend ostream& operator<<(ostream&, const EmployeePay&);
  124. friend istream& operator>>(istream&, EmployeePay&);
  125.  
  126. private:
  127. double annualPay;
  128. double monthlyPay;
  129. unsigned int dependents;
  130.  
  131. void validateDependents();
  132. void validateAnnualPay();
  133.  
  134. public:
  135. void GetFileData(ifstream& file);
  136. void PrintData();
  137. // Overloaded operators
  138. //const EmployeePay& operator =(const EmployeePay&);
  139.  
  140. }; //end class EmployeePay
  141.  
  142. #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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC