Using Custom vector template to change existing project from using arrays to vectors

We're taking a pre-existing project and adding a custom made vector class template to it. Thereby going through and changing everything that was declared as an array into something that deal with the custom made vector. IE: Changing an array of pointers into a vector of pointers

My problem is that I'm running into a bunch of dead end (dead end to me cause I'm a greenie at this still)errors. Here's what I have so far in my driver code
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// File Prologue
// Name: Nathan Kelsey
// CS 1410 Section 001
// Project: Project 08
// Date: 03/01/2013
// 
// I declare that the following code was written by me or provided 
// by the instructor for this project. I understand that copying source
// code from any other source constitutes cheating, and that I will receive
// a zero on this project if I am found in violation of this policy.

#include "EmployeeClass.h"
#include "Salaried.h"
#include "Hourly.h"
#include "MyVector.h"

#include <string>
#include <iostream>
#include <fstream>

using namespace std;

const int ONE = 1;
const int TWO = 2;
const int THREE = 3;
const int SIZE = 4;
//variables
int answer = 0;
string fileName;
string response;
bool exitLoop = true;

// printCheckSalaried function prologue
// Purpose: Display necessary employee data for a paycheck/pay stub
// Parameters: EmployeeClass object passed as const reference
// Returns: None
void printCheckSalary(const Salaried&);

// printCheckHourly function prologue
// Purpose: Display necessary employee data for a paycheck/pay stub
// Parameters: Hourly object passed as const reference
// Returns: None
void printCheckHourly(const Hourly&);
int main()
{
	// Fix Numerical Output data to two places after the decimal
	cout.setf(ios::fixed);
    cout.precision(2);
	
	// Create an array of Employee Pointers
	MyVector<EmployeeClass*> myEmployees;

	// Opening User Interface
	cout <<"This program has two functions." << endl;
	cout <<"Press 1 to create a data file." << endl;
	cout <<"Press 2 to read from the file and print paychecks." << endl;
	cout <<"Input: ";
	cin >> answer;
	
	// User enters one, then execute following code
	if(answer == ONE)
	{
		ofstream employeeInput(fileName.c_str());
		do
		{
		// Open file for output	
		//Create fstream object
		startover:
		cout << "Please enter a file name:";
		cin.ignore();
		cin >> fileName;
		employeeInput.open(fileName.c_str());
			if(employeeInput)
			{
				exitLoop = false;
				break;
			}
			if(employeeInput.fail())//Check to see if the file opened
			{	
				cout << "\nInvalid file name:" << endl;
				cout << "\nWould you like to try again? ";
				cin.ignore();
				cin >> response;
			}
			while (response.compare("Y") != 0 && response.compare("y") != 0 && response.compare("n") != 0 && response.compare("N") != 0);
			{
				  if (response == "y" || response == "Y")
				{
					  goto startover;
				}
				else 
				{
					  cout << "\nThank you and Goodbye." << endl;
					  system("PAUSE");
					  exit(1);
				}
			}
		}while(exitLoop);
		// write the data 
		//The only place you have to worry about throwing an exception in this assignment is when you find that you cannot read data from the file.
		myEmployees.push_back(new Hourly(1, "H. Potter", "Privet Drive", "201-9090", 40, 12.00));
		myEmployees.push_back(new Salaried(2, "A. Dumbledore", "Hogewarts", "803-1230", 1200));
		myEmployees.push_back(new Hourly(3, "R. Weasley", "The Burrow", "892-2000", 40, 10.00));
		myEmployees.push_back(new Salaried(4, "R. Hagrid", "Hogwarts", "910-8765", 1000));

		try
		{
			for(int i = 0; i < myEmployees.size(); i++)
			{
				// Utilizing pointer array, write the data to the file
				myEmployees.at(vectorData)->writeData(employeeInput);
			}
		}
		catch(EmployeeExceptions e)
		{
			cout <<"	\n\nThere was a file error while writing employee data";
			cout << "\nto the file. The data may not be saved correctly.";
			cout << "\nCheck your output file";
		}
	
		cout << "\nWrite Successful!!\n" << endl;
		employeeInput.close( ); 
		for(int i = 0; i < SIZE; i++)
		{
			delete myEmployees.size();
			myEmployees.at(vectorData) = NULL;
		}
		system("PAUSE");
	}
	if (answer == TWO)
	{
		//Create an object of the required kind, Hourly or Salaried.
    	//Save the pointer to this object in the new array of Employee pointers.
		//Have the object read its data from the file.
		//Repeat this for each set of data in the file. 
		ifstream employeeData(fileName.c_str());
		    do
			{
				startAgain:
				cout << "\nPlease enter in a filename: ";
				cin.ignore();
				cin >> fileName;   
				employeeData.open(fileName.c_str());
				if (employeeData)
				{
					exitLoop = false;
					goto readData;
					break;
				}
				if (employeeData.fail())
				{
					do
					{
						cout << "\nInvalid file name:" << endl;
						cout << "\nWould you like to try again? ";
						cin.ignore();
						cin >> response;
					}while (response.compare("Y") != 0 && response.compare("y") != 0 && response.compare("n") != 0 && response.compare("N") != 0);
					if (response == "y" || response == "Y")
					{
						goto startAgain;
					}
					else 
					{
						cout << "\nThank you and Goodbye."<< endl;
						system("PAUSE");
						exit(1);
					}
				}    
		} while (exitLoop);
						
			readData:
			
			string type;
			int index = 0;
				try
				{
					while(employeeData >> type)
					{
						if(type == "Hourly")
						{
							myEmployees.at(vectorData) = new Hourly();
						}
						else
						{
							myEmployees.at(vectorData) = new Salaried();
						}
						myEmployees.at(vectorData)->readData(employeeData);
						index++;	
					}
				}
			// Catch if Read Failed
				catch(EmployeeExceptions e)
				{
					if (e.getCode( ) == 1 || e.getCode( ) == 2)
					{
						cout << "\nError reading data, terminating program.";
						system("PAUSE");
						exit(1);
					}
					if (e.getCode( ) == 3)
					{
						cout << "\nUnexpected end of file, terminating program.";
						system("PAUSE");
						exit(1);
					}
				}
				int count = index;

				// if read was successful
				// call respective printCheck()
				for(int i = 0; i < count; i++)
				{
					system("CLS");
					EmployeeClass* employeePtr = myEmployees.at(vectorData);
					if(typeid( *myEmployees.at(vectorData)) == typeid(Hourly))
					{
						Hourly* hrlyPtr = dynamic_cast<Hourly*>(employeePtr);
						
							cout<<"\n------------------FluffShuffle Electronics------------------------\n";
							cout<<"\nPay to the order of "<<hrlyPtr->getEmployeeName()<<"..............................$"<<hrlyPtr->getCalcPay(); 
							cout<<"\n\nGlobal Bank of Computer Science Theory";
							cout<<"\n------------------------------------------------------------------\n";
							cout<<"Hours worked: "<< hrlyPtr->getWeeklyHours() <<"\n";
							cout<<"Hourly Wage: "<< hrlyPtr->getHourlyWage() << "\n" << endl;					
					}
					else if(typeid( *myEmployees.at(vectorData)) == typeid(Salaried)) 
					{
						Salaried* slryPtr = dynamic_cast<Salaried*>(employeePtr);

							cout<<"\n------------------FluffShuffle Electronics------------------------\n";
							cout<<"\nPay to the order of "<<slryPtr->getEmployeeName()<<"..............................$"<<slryPtr->getCalcPay(); 
							cout<<"\n\nGlobal Bank of Computer Science Theory";
							cout<<"\n------------------------------------------------------------------\n";
							cout<<"Weekly Salary: " <<slryPtr->getSalary() << endl;
					}
					system("PAUSE");
				}
			employeeData.close();//close the read file
	}
return 0;
}


This is what the teacher told us to do with this project:


1. To the list of #includes in your driver, add a #include for the header file for your vector class template.

2. Change the code in your diver that uses an array of Employee pointers so that it uses a vector of Employee pointers. Be sure that you are using your vector class template and not the Vector that is part of C++'s Standard Template Library. There are a few places in your code where you will have to be careful. For example, we did not overoad the [ ] operator in your vector class template, so you will have to use the at function to get access to an element in the vector.

I can go through and change all the array notation to myEmployees.at() but I'm at a complete loss as to what I put in the (). I've tried multiple things and end up with more headaches than success.

Here's one of roughly 30 errors I'm getting:

error C2227: left of '->writeData' must point to class/struct/union/generic type 111

Advice would be appreciative.
I can go through and change all the array notation to myEmployees.at() but I'm at a complete loss as to what I put in the ().


What type does your MyVector::at() method return?

What is vectorData and why is it the only value you use as an index?
I was only trying out vectorData. It's actually the pointer in my vector that points to the array in my vector

Here's the MyVector::at() method
1
2
3
4
5
6
7
8
9
10
template<class T>
T& MyVector<T>::at(T& n) const
{
	if(n < vSize)
	{
		return vectorData[n];
	}
	// Throw an exception if n is greater than vector size
	throw 10;   //This just a random number that fulfilled the exception requirement from a previous assignment
}
Last edited on
1
2
3
4
5
6
7
8
9
10
template<class T>
T MyVector<T>::at(unsigned n) const
{
	if(n < vSize)
	{
		return vectorData[n];
	}
	// Throw an exception if n is greater than vector size
	throw 10;   //This just a random number that fulfilled the exception requirement from a previous assignment
}


Note the change to the return type and parameter list. You don't index arrays with pointers, and since the method is const, returning a copy or a const reference makes sense, presumably you should also have a non-const version.

I don't think that'll fix your problem though. Were there errors that occurred earlier in the file? It's usually just the first two or so that you should worry about prior to attempting compilation again.
1
2
3
4
5
6
7
8
9
10
template<class T>
T MyVector<T>::at(unsigned n) const
{
	if(n < vSize)
	{
		return vectorData[n];
	}
	// Throw an exception if n is greater than vector size
	throw 10;   //This just a random number that fulfilled the exception requirement from a previous assignment
}


Note the change to the return type and parameter list. You don't index arrays with pointers, and since the method is const, returning a copy or a const reference makes sense, presumably you should also have a non-const version.

I don't think that'll fix your problem though. Were there errors that occurred earlier in the file? It's usually just the first two or so that you should worry about prior to attempting compilation again.
Topic archived. No new replies allowed.