c++ - Unsure about structures that use pointers and inheritance -
hi guys still pretty new coding give me grace, have error has structure sing contains object calling records. not sure if should make object pointer because when program compile has runtime error. error getting
undefined reference 'records::records()'
1d returned 1 exit status
the runtime error when use pointer records in employee structure happens first reference it.
since don't have commenting in program here explanation. clock in/clock out program. date class self explanatory. shift class implements , holds when employee clocks in , out. records class holds week work of shift objects , used write info spread sheet.
also said pretty new coding if have tips structure of program, or of sort love hear them.
main.cpp
*
#include <iostream> #include <cstdlib> #include <windows.h> #include <conio.h> #include <stdio.h> #include <limits> #include <fstream> #include <ctime> using namespace std; #include "date.h" #include "shift.h" #include "records.h" bool adminmenu(int id); int employeemenu(int id); void printinfo(int id); int findemptyspot(); void addemployee(); void printemployeelist(); void generateemployees(); void degenerateemployees(); date blankdate(1,1,1900,0,0); struct employee{ string name; bool adminstatus; int id; double wage; records r; }; typedef struct employee employee; employee data[100]; ifstream employeelist ("employeelist.txt"); int main() { bool x = true; if (employeelist.is_open()) { generateemployees(); employeelist.close(); } else cout << "unable open file"; sleep(1000); while(x) { int input,i=0; cout<< "enter 4 digit id: "; cin>> input; cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); std::system("cls"); while(data[i].id!=input) i++; ; if(data[i].adminstatus ==true) x = adminmenu(i); else employeemenu(i); } employeelist.close(); degenerateemployees(); return 0; } bool adminmenu(int placeinarray) { while(true) { time_t = time(0); // current time struct tm* tm = localtime(&now); date blankdate(1,1,1900,0,0); date currentdate(tm->tm_mon,tm->tm_mday,tm->tm_year,tm->tm_min,tm->tm_sec); std::system("cls"); int input=0; cout<<"you in admin menu\n1. print info\n2. add employee\n3. edit employee\n4. delete employee\n5. print employee list\n6. clock in\n7. clock out\n8. close program\n9. finish"<<endl; cin>> input; cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); if(input<1||input>9) { cout<< "invalid input please try again"<<endl; sleep(1000); std::system("cls"); } else if(input==1) { std::system("cls"); printinfo(placeinarray); getch(); std::system("cls"); } else if(input ==2) { addemployee(); } else if (input ==3) { std::system("cls"); int placetoedit, whattoedit; cout << "please @ list of employees below , enter number of employee edit"; printemployeelist(); cin>> placetoedit; std::system("cls"); cout<< "what want change? \n1. name\n2. id\n3. admin status\n4. clock in/out"; cin>> whattoedit; if(whattoedit==1) { std::system("cls"); cout<<"enter employee's new name: "; cin >> data[placetoedit].name; cout<< "\nemployee's name adjusted successfully."; sleep(1000); } else if(whattoedit==2) { std::system("cls"); cout<<"enter employee's new id: "; cin >> data[placetoedit].id; cout<< "\nemployee's id adjusted successfully."; sleep(1000); } else if(whattoedit == 3) { std::system("cls"); cout<<"enter employee's new admin status: "; cin >> data[placetoedit].adminstatus; cout<< "\nemployee's admin status adjusted successfully."; sleep(1000); } else if(whattoedit == 4) { /* std::system("cls"); int status; cout<<"would clock them in or out?(0 or 1)"; cin >> status;*/ } else { std::system("cls"); cout<< "error: invalid input. returned admin menu"; sleep(1000); } } else if(input ==4) { std::system("cls"); int placetoedit; cout << "please @ list of employees below , enter number of employee delete"; printemployeelist(); cin>> placetoedit; if( data[placetoedit].id!= 0) { data[placetoedit].name = '\0'; data[placetoedit].id = '\0'; data[placetoedit].adminstatus='\0'; cout<< "\nemployee deleted."; sleep(1000); } else cout<< "error: invalid input. returned admin menu"; } else if(input ==5) { printemployeelist(); sleep(1000); } else if (input == 6) { if(tm->tm_wday==1) { data[placeinarray].r.monday.shift::setbeginofshift(blankdate); } else if(tm->tm_wday==2) { data[placeinarray].r.tuesday.shift::setbeginofshift(blankdate); } else if(tm->tm_wday==3) { data[placeinarray].r.wednesday.shift::setbeginofshift(blankdate); } else if(tm->tm_wday==4) { data[placeinarray].r.thursday.shift::setbeginofshift(blankdate); } else if(tm->tm_wday==5) { data[placeinarray].r.friday.shift::setbeginofshift(blankdate); } std::system("cls"); cout<< "successfully clocked in at"; currentdate.date::print(); } else if (input == 7) { if(tm->tm_wday==1) { data[placeinarray].r.getmonday().setendofshift(currentdate); } else if(tm->tm_wday==2) { data[placeinarray].r.gettuesday().setendofshift(currentdate); } else if(tm->tm_wday==3) { data[placeinarray].r.getwednesday().setendofshift(currentdate); } else if(tm->tm_wday==4) { data[placeinarray].r.getthursday().setendofshift(currentdate); } else if(tm->tm_wday==5) { data[placeinarray].r.getfriday().setendofshift(currentdate); } } else if(input ==8) { std::system("cls"); return false; } else if(input ==9) { std::system("cls"); return true; } } } int employeemenu(int placeinarray) { int input; while(true) { time_t = time(0); // current time struct tm* tm = localtime(&now); date blankdate(1,1,1900,0,0); date currentdate(tm->tm_mon,tm->tm_mday,tm->tm_year,tm->tm_min,tm->tm_sec); cout<< "you in employee menu\n1. print info\n2. clock in\n3. clock out\n9. finish"; cin>> input; std::system("cls"); if(input<1||input>9) { cout<< "invalid input please try again"<<endl; sleep(1000); std::system("cls"); } else if(input==1) printinfo(placeinarray); else if (input == 2) { if(tm->tm_wday==1) { data[placeinarray].r.getmonday().setbeginofshift(currentdate); } else if(tm->tm_wday==2) { data[placeinarray].r.gettuesday().setbeginofshift(currentdate); } else if(tm->tm_wday==3) { data[placeinarray].r.getwednesday().setbeginofshift(currentdate); } else if(tm->tm_wday==4) { data[placeinarray].r.getthursday().setbeginofshift(currentdate); } else if(tm->tm_wday==5) { data[placeinarray].r.getfriday().setbeginofshift(currentdate); } } else if (input == 3) { if(tm->tm_wday==1) { data[placeinarray].r.getmonday().setendofshift(currentdate); } else if(tm->tm_wday==2) { data[placeinarray].r.gettuesday().setendofshift(currentdate); } else if(tm->tm_wday==3) { data[placeinarray].r.getwednesday().setendofshift(currentdate); } else if(tm->tm_wday==4) { data[placeinarray].r.getthursday().setendofshift(currentdate); } else if(tm->tm_wday==5) { data[placeinarray].r.getfriday().setendofshift(currentdate); } } else if(input ==9) { std::system("cls"); return 0; } } } void printinfo(int placeinarray) { cout<<"name: "<<data[placeinarray].name<<endl; cout<<"id: "<<data[placeinarray].id<<endl; cout<<"admin status: "<< data[placeinarray].adminstatus<<""<<endl; } int findemptyspot() { int i=0; while(data[i].id!=0) i++; return i; } void printemployeelist() { int x; for( x = 0; x<100; x++) { if( data[x].id!=0) cout<<"\n"<<x<<". "<<data[x].name<<endl; } } void addemployee() { int newplace = findemptyspot(); std::system("cls"); cout<<" enter employee's name: "; cin >> data[newplace].name; cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); cout<<" enter employee's id: "; cin >> data[newplace].id; cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); cout<<" enter employee's admin status: "; cin >> data[newplace].adminstatus; cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); std::system("cls"); cout<< "your new employee profile has been created!"; printinfo(newplace); } void generateemployees() { string n; bool a; int i, j =0; while (employeelist >>n>>i>>a ) { data[j].name=n; data[j].adminstatus=a; data[j].id=i; j++; } } void degenerateemployees() { ofstream employeelist("employeelist.txt",ios::trunc); int x; for( x = 0; x<100; ++x) { if( data[x].id!=0) employeelist<< data[x].name<< " "<< data[x].id<< " "<< data[x].adminstatus<< "\n";`` } employeelist<< "end 0 0"; employeelist.close(); }
*
date.cpp
#include <iostream> #include "date.h" // include date class definition using namespace std; // constructor confirms proper value month; calls // utility function checkday confirm proper value day //------------------------------------------------------------- date::date( int mn, int dy, int yr, int hr, int minu) { month = mn; year = yr; day = dy ; hour = hr; minute=minu; } // end date constructor date::date (const date & datetocopyfrom) // copy constructor { month = datetocopyfrom.month; day = datetocopyfrom.day; year = datetocopyfrom.year; hour = datetocopyfrom.hour; minute = datetocopyfrom.minute; } date & date::operator = (const date & other) // assignmenrt operator { this->month = other.month; this->day = other.day; this->year = other.year; this->hour = other.hour; this->minute= other.minute; return *this; } /* setdate date user */ void date::setdate() { bool flag = false; int mn,dy,yr,hr,minu; cin.clear(); { cout << "(month day year hour minute):\n"; cin >> mn >> dy >> yr >> hr >> minu; year = yr; month = mn; day = dy; hour = hr; minute=minu; if(yr < 1900) { cout << "cannot handle dates prior 1900 ad" << endl; cout << "try again!" << endl; flag = false; } else if(!isvaliddate()) { cout << "invalid date" << endl; cout << "try again!" << endl; flag = false; } else flag = true; } while (!flag); }//end date constructor // print date object in form month/day/year //------------------------------------------------------------- void date::print() const { std::cout << hour<<":"<<minute<< "on"<< month << '/' << day << '/' << year; } // end function print // output date object show when destructor called //------------------------------------------------------------- date::~date() { } // end ~date destructor // utility function confirm proper day value based on // month , year; handles leap years, //------------------------------------------------------------- int date::checkday( int testday ) const { static const int dayspermonth[ monthsperyear + 1 ] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // determine whether testday valid specified month if ( testday > 0 && testday <= dayspermonth[ month ] ) return testday; // february 29 check leap year if ( month == 2 && testday == 29 && ( year % 400 == 0 || ( year % 4 == 0 && year % 100 != 0 ) ) ) return testday; std::cout << "invalid day (" << testday << ") set 1.\n"; return 1; // leave object in consistent state if bad value } // end function checkday bool date::operator>(const date &dateonright) // greater operator { bool greater; int year = dateonright.getyear(); if(dateonright.getyear() > year) greater = false; else if(dateonright.getyear() == year && dateonright.getmonth() > month) greater = false; else if(dateonright.getyear() == year && dateonright.getmonth() == month && dateonright.getday() >= day) greater = false; else if(dateonright.getyear() == year && dateonright.getmonth() == month && dateonright.getday() == day&& dateonright.gethour()>= hour) greater = false; else if(dateonright.getyear() == year && dateonright.getmonth() == month && dateonright.getday() == day&& dateonright.gethour()== hour&& dateonright.getminute()<= minute) greater = false; else greater = true; return greater; } // add 1 year date void date::addyears(int nyears) { year = year + nyears; //month = month; if (day == 29 && !isleapyear(year)) { day = 28; }//end if } /***********************************************/ bool date::isleapyear(int year) { bool leapyear = false; if (year % 400 == 0) { leapyear = true; } else { if (year % 100 == 0) { leapyear = false; } else { if (year % 4 == 0) leapyear = true; else leapyear = false; } } // cout << "leapyear " << leapyear << endl; return leapyear; } /************************************************/ bool date::isvaliddate() { bool validdate; int monthdays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if(month < 1 || month > 12 || day < 0 || !((isleapyear(year) && month == 2 && day <= 29) || (day <= monthdays[month - 1]))) validdate = false; else validdate = true; // cout << "validdate " << validdate << endl; return validdate; }
date.h
#ifndef date_h #define date_h class date { public: static const int monthsperyear = 12; // number of months in year date (int mn=1, int dy=1, int yr=1900, int hr=0, int minu =0); // constructors date (const date& datetocopyfrom); // copy constructor date & operator = (const date & other); // assignmenrt operator void setdate (); int getyear() const {return(year);}; int getmonth() const {return (month);}; int getday() const {return (day);}; int gethour() const {return (hour);}; int getminute() const {return (minute);}; void print() const; // print date in month/day/year format bool operator>(const date &dateonright); void addyears(int nyears); ~date(); // destructor provided confirm destruction order private: int month; // 1-12 (january-december) int day; // 1-31 based on month int year; // year int hour; int minute; // utility function check if day proper month , year int checkday( int testday ) const; bool isleapyear(int year); bool isvaliddate(); }; #endif
shift.cpp
#ifndef date_h #define date_h class date { public: static const int monthsperyear = 12; // number of months in year date (int mn=1, int dy=1, int yr=1900, int hr=0, int minu =0); // constructors date (const date& datetocopyfrom); // copy constructor date & operator = (const date & other); // assignmenrt operator void setdate (); int getyear() const {return(year);}; int getmonth() const {return (month);}; int getday() const {return (day);}; int gethour() const {return (hour);}; int getminute() const {return (minute);}; void print() const; // print date in month/day/year format bool operator>(const date &dateonright); void addyears(int nyears); ~date(); // destructor provided confirm destruction order private: int month; // 1-12 (january-december) int day; // 1-31 based on month int year; // year int hour; int minute; // utility function check if day proper month , year int checkday( int testday ) const; bool isleapyear(int year); bool isvaliddate(); }; #endif
shift.h
#include "date.h" #ifndef shift_h #define shift_h using namespace std; class shift: public date { public: //constructor shift();//blankdate,blankdate,(9999),blankdate); shift(date in, date out, int identity){beginofshift=in; endofshift=out; ident = identity;}; void setbeginofshift(date x); void setendofshift(date x); date getbeginofshift() const{return (beginofshift);}; date getendofshift() const{return (endofshift);}; date getlength(); int getid() const{return (ident);}; ~shift(); private: date beginofshift; date endofshift; date length; int ident; }; #endif // shift_h
records.cpp
#include<iostream> using namespace std; #include "records.h" #include "shift.h" extern date blankdate; void records::setbeginningofweek(date x) { getbeginningofweek()=x; } void records::setmonday(shift x) { getmonday()=x; } void records::settuesday(shift x) { gettuesday()=x; } void records::setwednesday(shift x) { getwednesday()=x; } void records::setthursday(shift x) { getthursday()=x; } void records::setfriday(shift x) { getfriday()=x; } records::~records() { }
records.h
#include "shift.h" #ifndef records_h #define records_h using namespace std; class records: public shift { public: records(date x){beginningofweek=x;}; records(); void setbeginningofweek(date x); void setmonday(shift x); void settuesday(shift x); void setwednesday(shift x); void setthursday(shift x); void setfriday(shift x); date getbeginningofweek() const{return(beginningofweek);}; shift getmonday() const{return(monday);}; shift gettuesday() const{return(tuesday);}; shift getwednesday() const{return(wednesday);}; shift getthursday() const{return(thursday);}; shift getfriday() const{return(friday);}; ~records(); date beginningofweek; shift monday; shift tuesday; shift wednesday; shift thursday; shift friday; }; #endif // records_h
seems declared constructor records, didn't define it.
Comments
Post a Comment