No match for 'operator[]'

I have an assignment to create a database using structs. It's not completed at the moment but whenever i try to compile it tells me "no match for 'operator[]' in 'db[count]'" or "'db[nEmployees]'". I've scoured several help sites, and my textbook to no avail, so I can only assume i've made some incredibly noobish and hideous mistake. Can anyone enlighten me?
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
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <string>
#define byID 1
#define byNAME 2
#define dbSIZE 100
#define idSIZE 5
#define nameSIZE 12
#define emailSIZE 20
using namespace std;
struct Employee{
       char id[idSIZE + 1];
       char firstName[nameSIZE + 1];
       char lastName[nameSIZE + 1];
       char email[emailSIZE + 1];
       double salary;
};

void createRecord(char *id, char *firstName, char *lastName, char *email, double salary, int &nEmployees);

void output(Employee db, int nEmployees) {
     cout.setf(ios::fixed | ios::showpoint);
     cout.precision(2);
     cout << "No. EmpID Employee Name        Email                Salary" << endl;
     cout << "--- ----- -------------------- -------------------- ----------" << endl;
     for (int count = 0; count<nEmployees; count++){
         cout << count << "    ";
         cout << db[count].id << " " << setfill(' ') << setw(20) << db[count].firstName << " " << db[count].lastName;
         cout << db[count].email << setw(10) << db[count].salary;
         }
}
void createRecord(char *id, char *firstName, char *lastName, char *email, double salary, Employee& db, int &nEmployees){
     
     strcpy(db[nEmployees].id, id);
	 strcpy(db[nEmployees].firstName, firstName);
	 strcpy(db[nEmployees].lastName, lastName);
	 strcpy(db[nEmployees].email, email);
	 db[nEmployees].salary = salary;
	 
	 nEmployees++;
}    
void initialize(Employee& db, int &nEmployees){
     cout.setf(ios::fixed | ios::showpoint);
     cout.precision(2);
     createRecord("00101", "Paula", "Brown","pb@aol.com", 1000, nEmployees);
     createRecord("00102", "Paul", "Green","pg@aol.com", 2700.45, nEmployees);   
     createRecord("00203", "Chris", "Reddy","cr@aol.com", 2304.67, nEmployees);
     createRecord("00204", "Christine", "Bluer","cb@aol.com", 40009.99, nEmployees);
     createRecord("00305", "Stephen", "Black", "sb@aol.com", 8500.01, nEmployees);
     createRecord("00306", "Wei", "Martin", "wm@aol.com", 98999.99, nEmployees);
     createRecord("00307", "Mitch", "Martinez", "mitch@aol.com", 78999.88, nEmployees);
     createRecord("00408", "David", "Boyle", "db@aol.com",100000.01, nEmployees);
     createRecord("00409", "Chris", "Boyle", "cb@aol.com",200000.01, nEmployees);
}
void menu(int& choice, int nEmployees){
     cout << "********** Employee Database ******* Total Number of Employees = " << nEmployees << endl;
     cout << "1. Add an employee" << endl;
     cout << "2. Display all employees" << endl;
     cout << "3. Search employee by ID" << endl;
     cout << "4. Search employee by Last Name" << endl;
     cout << "5. Delete employee by ID" << endl;
     cout << "6. Delete employee by last Name" << endl;
     cout << "7. Exit" << endl;
     cin >> choice;
     if (choice<1 || choice>7){
        cout << "Try again" << endl;
        cin >> choice; }
     };
void input(){
     char *id;
     char *firstName;
     char *lastName;
     char *email;
     double salary;
     cout << "Please input a 5 digit ID" << endl;
     cin >> id;
     cout << "Please input a first name" << endl;
     cin >> firstName;
     cout << "Please input a last name" << endl;
     cin >> lastName;
     cout << "Please input an email" << endl;
     cin >> email;
     cout << "Please input a salary" << endl;
     cin >> salary;
     createRecord(id, firstName, lastName, email, salary);
}          
int main (){
    Employee db[100];
	int nEmployees = 0;
	bool operate = true;
	int choice = 0;
	
	initialize(db, nEmployees);
	while(operate == true){
        menu(choice, nEmployees);
        switch (choice){ 
            case 0:
                 cout << "You done goof'd";
                 break;
            case 1:
                 input(); 
                 break;
            case 2:
                 output(db, nEmployees);
                 break;
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
                 operate = false;
                 break;
     }
}
    system("PAUSE");
    return 0;
}


Last edited on
Employee& db is a reference to a single Employee named db. It is not an array, and cannot be indexed like one.

You might try Employee* db, or Employee db[] in your parameter lists. They're equivalent.




That did the trick! Thank you!
Topic archived. No new replies allowed.