| | |
HELP: class static function - compile errors
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2003
Posts: 8
Reputation:
Solved Threads: 0
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.
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,
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.
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
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.
C++ Syntax (Toggle Plain Text)
static Employee* empArray[];
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,
C++ Syntax (Toggle Plain Text)
empArray[numEmployees] = new 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.
C++ Syntax (Toggle Plain Text)
//// 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
Cheers
![]() |
Similar Threads
- Compile errors [help] [ log ] (C++)
- Reading an input file as a class memeber function (C++)
- Abstract Class member function problem (C++)
- static function problem (C++)
- static function? help (C++)
Other Threads in the C++ Forum
- Previous Thread: Microsoft Visual C++ Runtime Library Error
- Next Thread: c++ code help
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





