Invilad types 'double[int]' for array subscript

So i am trying to make a program that will take in employee info and then display it out but i am stuck at this error

1
2
Employee.cc: In member function ‘void HourlyWorker::setPay(double, double)’:
Employee.cc:45: error: invalid types ‘double[int]’ for array subscript


here are my files

Employee.h
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
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <iomanip>
#include <unistd.h>
#include <stdlib.h>
using namespace std; 

class Employee // Base class
{
    public:
        Employee(const string, const string, const int);
        virtual void print() const = 0;
        virtual float earnings() const = 0;
        ~Employee();
        int pos;
    private:
        string firstName;
        string lastName;
};

class HourlyWorker:public Employee
{
    public:
        HourlyWorker(const string, const string, const int);
        void print() const;
        void setPay(double, double);
        float earnings() const;
    private:
        double hourlyRate[10];
        double hours[10];
};

class Manager:public Employee
{
    public:
        Manager(const string, const string, const int);
        void print() const;
        void setPay(double);
        float earnings() const;
    private:
        double monthlyPay[10];
};

class OnCommission:public Employee
{
    public:
        OnCommission(const string, const string, const int);
        void print() const;
        void setPay(int, double);
        float earnings() const;
    private:
        float salary[10];
        double commission[10];
        int quantity[10];
};


Employee.cc
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
#include "Employee.h"

Employee::Employee(string first, string last, int pos2)
{
    firstName = first;
    lastName = last;
    pos = pos2;    
}

Employee::~Employee()
{
    //will do something dont know what yet though.
}

HourlyWorker::HourlyWorker(string first, string last, int pos2) : Employee(first, last, pos2)
{
}

Manager::Manager(string first, string last, int pos2) : Employee(first, last, pos2)
{
}

OnCommission::OnCommission(string first, string last, int pos2) : Employee(first, last, pos2)
{
}
void Employee::print() const
{
    cout << firstName << " " << lastName << endl;
}

float Employee::earnings() const
{
    
}

void HourlyWorker::print() const
{
    Employee::print();
    cout << " " << " earns " << earnings() << endl;
}

void HourlyWorker::setPay(double hours, double rate)
{
    hourlyRate[pos] = rate;
    hours[pos] = hours;
}

float HourlyWorker::earnings() const
{
    return hourlyRate[pos]*hours[pos];
}

void Manager::print() const
{
    Employee::print();
    cout << " earns " << earnings() << endl;
}

void Manager::setPay(double money)
{
    monthlyPay[pos] = money;
}

float Manager::earnings() const
{
    return monthlyPay[pos];
}

void OnCommission::print() const
{
    Employee::print();
    cout << " has wages " << earnings() << endl;
}

void OnCommission::setPay(int items, double rate)
{
    commission[pos] = rate;
    quantity[pos] = items;
}

float OnCommission::earnings() const
{
    return salary[pos] + commission[pos]*quantity[pos];
}


Employee_driver.cc
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "Employee.h"

int read_int();
double read_double();

int main() 
{
    int pos = 0;
    int temp;
    int answer = 0;
    string firstname[10];
    string lastname[10];
    int classification[10];
    int itemsMade = 0;
    double managerSalary = 0;
    double hourlyRate = 0;
    double hoursWorked = 0;
    double itemRate = 0;
    bool ex = false;
    
    Employee *arrayOfEmp[10];
    
    while (ex != true)
    {
        cout << "1. Add new Employee" << endl;
        cout << "2. List all Employees" << endl;
        cout << "3. Exit" << endl;
        answer = read_int();        
        
        if (answer == 1)
        {

            cout << "What is the classification of this worker? " << endl;
            cout << "1. Manager" << endl;
            cout << "2. Hourly Worker" << endl;
            cout << "3. Contract Worker" << endl;
            classification[pos] = read_int();

            if (classification[pos] == 1)
            {
                cout << "What is the first name of the employee:"; 
                cin >> firstname[pos];

                cout << "What is the last name of the employee:";
                cin >> lastname[pos];

                cout << "How much is the Salary that this Manager gets:";
                managerSalary = read_double();
                
                Manager m(firstname[pos], lastname[pos], pos);
                
                m.Manager::setPay(managerSalary);
        
                arrayOfEmp[pos] = &m;
                
            }
            else if (classification[pos] == 2)
            {
                cout << "What is the first name of the employee:"; 
                cin >> firstname[pos];

                cout << "What is the last name of the employee:";
                cin >> lastname[pos];

                cout << "What is this workers hourly pay:";
                hourlyRate = read_double();
                
                cout << "How many hours did this worker work:";
                hoursWorked = read_double();
                
                HourlyWorker h(firstname[pos], lastname[pos], pos);
                
                h.HourlyWorker::setPay(hourlyRate, hoursWorked);
        
                arrayOfEmp[pos] = &h;
            }
            else if (classification[pos] == 3)
            {
                cout << "What is the first name of the employee:"; 
                cin >> firstname[pos];
    
                cout << "What is the last name of the employee:";
                cin >> lastname[pos];                
                
                cout << "How many items did this Contracter produce:";
                itemsMade = read_int();

                cout << "What is this Contractors price per item:";
                itemRate = read_double();
                
                OnCommission o(firstname[pos], lastname[pos], pos);
                
                o.OnCommission::setPay(itemsMade, itemRate);
        
                arrayOfEmp[pos] = &o;
        
            }
            pos++;
        }//end big if 
        
        else if (answer == 2)
        {
    
            int tempPos = 0;
                
            for (int i = 0;i < tempPos; i++)
            {
                arrayOfEmp[i] -> print();
            }
    
        } 
        
        else if (answer == 3)
        {
            ex = true;
            exit(1);
        }    
    }//end loop
}

int read_int()
{ 
//Preconditions: none
//Postconditions: the users input was a valid integer
    int val; //integer that will be used to check and see if the users input is valid
    cin >> val;
    while( !cin.good () ){ //while loop that if the value is not valid will ask them to input a valid answer
        cin.clear();
        cin.ignore(256, '\n');
        cout <<"invalid number" << endl;
        cin >> val;
        }
return val;
}
double read_double()
{ 
//Preconditions: none
//Postconditions: the users input was a valid integer
    double val; //integer that will be used to check and see if the users input is valid
    cin >> val;
    while( !cin.good () ){ //while loop that if the value is not valid will ask them to input a valid answer
        cin.clear();
        cin.ignore(256, '\n');
        cout <<"invalid number" << endl;
        cin >> val;
        }
return val;
}


any help or insight on this error would be great

-thanks
Last edited on
You are trying to assign hours to hours[pos] because a function parameter shadows a class variable
This should fix the problem:

1
2
3
4
5
void HourlyWorker::setPay(double hours, double rate)
{
    hourlyRate[pos] = rate;
    this->hours[pos] = hours; // this->hours is the member of HourlyWorker while hours is a parameter of the function
}
Last edited on
Topic archived. No new replies allowed.