Novice having problem with payroll program

The following program is part of an assignment using a base class and derived classes to calculate payroll.

When I compile I get the message:
[Linker error] undefined reference to `vtable for payroll'
C:\DOCUME~1\will\LOCALS~1\Temp\ccCmbaaa.o(.text+0x388) In function `ZN7payrollD1Ev':
[Linker error] undefined reference to `vtable for payroll'
C:\DOCUME~1\will\LOCALS~1\Temp\ccCmbaaa.o(.text+0x388) ld returned 1 exit status

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

class payroll {
ifstream fin;
public: char employeename[14], maritalstatus;
        int employeeid, n;
        float taxrate;
        double hoursworked, overtimehours, regularhours;
        double hourlyrate, regularpay, totalnetpay, annualpay;
        double taxamount, avgnetpay, netpay, grosspay, overtimepay;
        virtual void findgrosspay();
        void findtaxamount();
        void findnetpay();
        void printheaders();
        void printdata();
        void printavgnet();
        payroll();
        ~payroll();
        void printreport(); };

class salaried : public payroll {
public: void findgrosspay() {
             overtimehours = 0;
             regularpay = 0;
             overtimepay = 0;
             grosspay = 0;
             if (hoursworked > 0) {
                 overtimepay = hoursworked * (regularpay / 52 / 40);
                 regularpay = hourlyrate / 52;
             }//If
             grosspay = regularpay + overtimepay;
} };

class hourly : public payroll {
public: void findgrosspay() {
             overtimehours = 0;
             regularpay = 0;
             overtimepay = 0;
             grosspay = 0;
             if (hoursworked > 40 ) {
                 overtimehours = hoursworked - 40;
                 regularpay = hoursworked * hourlyrate;
                 overtimepay = overtimehours * (hourlyrate * 1.5);
                 grosspay = regularpay + overtimepay; }
             else
                 grosspay = hoursworked * hourlyrate;
                 regularpay = grosspay; 
} };

payroll::payroll() {
fin.open("payroll.dat"); }
payroll::~payroll() {
fin.close();  }

//void payroll::findgrosspay() {
//     overtimehours = 0;
//     regularpay = 0;
//     overtimepay = 0;
//     grosspay = 0;
//     if (hoursworked > 40 ) {
//        overtimehours = hoursworked - 40;
//        regularpay = hoursworked * hourlyrate;
//        overtimepay = overtimehours * (hourlyrate * 1.5);
//        grosspay = regularpay + overtimepay; }
//     else
//        grosspay = hoursworked * hourlyrate;
//        regularpay = grosspay; 
//}// findgrosspay

void payroll::findtaxamount() {
     taxrate = .30;
     taxamount = grosspay * taxrate;
}// findtaxamount

void payroll::findnetpay()  {
     netpay = grosspay - taxamount;
     totalnetpay = totalnetpay + netpay;
}// findnetpay

void payroll::printavgnet() {
     avgnetpay = totalnetpay / n;
     cout<<endl<<"The average net pay for "<<n <<" employees is "
     <<setw(8)<<setprecision(2)<<fixed<<left<<showpoint<<avgnetpay<<endl;
}// printavgnet
     
void payroll::printheaders()  {
     cout<<setw(45)<<"-PAYROLL REPORT-"<<endl<<endl;
	 cout<<" NAME      ID   HW   OT    RT-PAY  OT-PAY   GROSS"
		   "    TAX   NETPAY"<<endl;
cout<<"-----------------------------------------------------------------"<<endl; 
}//printheaders

void payroll::printdata()    {
 	 cout<<setw(10)<<left<<employeename<<setw(4)<<employeeid;
     cout<<setprecision(2)<<setiosflags(ios::fixed | ios::showpoint);
 	 cout<<setw(6)<<right<<hoursworked<<setw(5)<<overtimehours<<setw(8)
     <<regularpay<<setw(8)<<overtimepay<<setw(8)<<grosspay<<setw(8)
     <<taxamount<<setw(8)<<netpay<<endl;  
}//printdata

void payroll::printreport()    {
     n = 0;
     printheaders();
     while (fin>>employeename>>employeeid>>maritalstatus>>hoursworked>>hourlyrate) {
           if (employeeid < 9000) 
               hourly findgrosspay();
           else
               salaried findgrosspay();
           findtaxamount();
           findnetpay();
           printdata();
           n++;
     }// While
     printavgnet();
}// printreport

int main() {
    payroll employee;
    employee.printreport();
} //main 


I was progressing nicely until now. Can someone point me in the right direction?

Many thanks.
Last edited on
Hello,

I compile the code using VStudio 2005 and get an error on the line

hourly findgrosspay();

I assume you're trying to call the findgrosspay method on the hourly class. You have to instantiate the class as an object before invoking the function.
Isn't that what I did here:

class hourly : public payroll {
public: void findgrosspay() {
overtimehours = 0;
regularpay = 0;
overtimepay = 0;
grosspay = 0;
if (hoursworked > 40 ) {
overtimehours = hoursworked - 40;
regularpay = hoursworked * hourlyrate;
overtimepay = overtimehours * (hourlyrate * 1.5);
grosspay = regularpay + overtimepay; }
else
grosspay = hoursworked * hourlyrate;
regularpay = grosspay;
} };

findgrosspay() is declared a virtual function in the base class and defined in the subclasses hourly and salaried.
Last edited on
There are two issues

1) hourly findgrosspay(); is not the correct syntax for calling a member function on a class. If you want to call hourly memberfunction you have to be explicit hourly::findgrosspay(); by using the ::

2) You need to uncomment the void payroll::findgrosspay() function definition otherwise you'll get unresolved external for the findgrosspay on the payroll class. The reason is because you defined the function as virtual which means there has to be a definition on the payroll class. If you don't want to implement on the payroll class and force inherited class to implent than use pure virtual by replacing
virtual void findgrosspay();
with
virtual void findgrosspay() = 0;
Got it to compile without errors.

Thanks for your help.
Are you in my same class with SUNY
Is it ESC? If so, quite so.
I've been pointed in the right direction. I will set out to restructure the code. I've received a very good sample of what it should look like. This is how we learn.

Thanks all!
This is for week 4 right. I am surprised how many lines. I am bearly using 49 lines. And yes ESC here as well.
Topic archived. No new replies allowed.