Multiple Errors - Class Structure

I am trying to get a program for class running and I cannot figure out what I am doing wrong. I keep getting multiple errors and quite frankly I don't really know what I am doing. Could anyone help me figure out what I am not declaring, what is in the wrong spot and why my program is overall written wrong?

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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326

#include <iostream>
#include <iomanip>
#include <string>


using namespace std;

class Seller

   {
   private:
   
      string FirstName;
      string LastName;
      string ID;
	  double SalesTotal;
      
   public:

      // Constructors
     Seller();                          // Default constructor
     Seller(string, string, string , double);
      
      // Accessor methods   
      void setFirstName(string);
      string getFirstName();
      void setLastName(string);
      string getLastName();
	  void setID(string);
	  string ID();
      void setSalesTotal(double);
      double setSalesTotal();
      
      // Other methods
      void print();
   };
   
class SalesRegion
	
	{
	private:
	
		double numSellers;
		
	public:
		
		SalesRegion();
		SalesRegion(string, string);
		
		void setRegionName(string);
		string getRegionName();
		void sortSellersByID();
		void findSellerID(string);
		void setRegionName(string);
		string getRegionName();
		void processSales(string);
		
		void print();
		
		
	};
	
	
Seller::Seller() //default constructor for the Seller Class
	{
	setFirstName("None");
	setLastName("None");
	setID("ZZZ000");
	setSalesTotal(0);
	}

Seller::Seller(string newFirstName, string newLastName, string newID, double newSalesTotal) //contructor for seller class
	{
	setFirstName(newFirstName);
	setLastName(newLastName);
	setID (newID);
	setSalesTotal(newSalesTotal);
	}

void Seller::setFirstName(string newFirstName) //check if the length of newFirstName is greater than 0
	{
	if (newFirstName.length() > 0)
		{
		firstName = newFirstName;
		}
		
	else
		firstName = "None";
	}

void Seller::setLastName(string newLastName) //check if the length of newLastName is greater than 0
	{
	if (newLastName.length() > 0)
		{
		lastName = newLastName;
		}
		
	else
		lastName = "None";
	}

void Seller::setID(string newID) //check if the length of newID is greater than 0
	{
	if (newID.length() > 0 && newID.length() < 7)
		{
		sellerID = newID;
		}
		
	else
		sellerID = "ZZZ000";
	}

void Seller::setSalesTotal(double newSalesTotal) //checks if the sales is greater than 0
	{
	if (newSalesTotal> 0)
		{
		salesTotal = newSalesTotal;
		}
		
	else
		salesTotal = 0;
	}

string Seller::getFirstName() //returns the seller's first name
	{
	return firstName;
	}

string Seller::getLastName() //returns the seller's last name
	{
	return lastName;
	}

string Seller::getID() //returns the seller's id
	{
	return sellerID;
	}

double Seller::getSalesTotal() //returns the seller's sales total
	{
	return salesTotal;
	}

void Seller::print()  //prints data members of seller into formatted columns
	{
	cout << fixed << setprecision(2);
	
	cout << left << setw (25) << lastName + ", " + firstName << " " <<  setw(9) << sellerID
		 << right << setw (10) <<salesTotal << endl;
	}

void SalesRegion::sortSellersByID() //sorts selers in ascending order by seller id
	{
	int i;
	int j; 
	int min;
	Seller temp;
   
	for (i = 0; i < numSellers - 1; i++)
    	{
    	min = i;
      
    	for (j = i+1; j < numSellers; j++)
        	{
        	if (sellerArray[j].getID() < sellerArray[min].getID())
            min = j;
        	}
         
		temp = sellerArray[i];
		sellerArray[i] = sellerArray[min];
		sellerArray[min] = temp;
		}
	}

int SalesRegion::findSellerID(string searchID) // finds the position of the seller array element with a seller ID that matches search id
	{
	int i;

	for (i = 0; i < numSellers; i++)
		{
		if (searchID == sellerArray[i].getID())
		return i;
		}

	return -1;
	}

SalesRegion::SalesRegion() //default constructor for the Sales Region Class
	{
	numSellers = 0;
	}

SalesRegion::SalesRegion(string newRegionName, string fileName) //constructor for the Sales Region Class, builds sales array
	{
	ifstream inFile;
	string firstName;
	string lastName;
	string sellerID;
	double salesTotal;
	
	numSellers = 0;
	setRegionName (newRegionName);
	
	inFile.open(fileName.c_str());
	if (!inFile)
		{
		cout << "Error - unable to open input file " << fileName << endl;
		exit(1);
		}
		
	inFile >> lastName;
	while (inFile)
		{
		inFile >> firstName;
		inFile >> sellerID;
		inFile >> salesTotal;
		
		Seller newSeller (firstName, lastName, sellerID, salesTotal);
		
		sellerArray [numSellers] = newSeller;
		numSellers++;
		
		inFile >> lastName;
		}
	
	inFile.close();
	
	sortSellersByID ();
	}
void SalesRegion::setRegionName(string newRegionName) //checks if the lenght of the newRegionName is greater than 0
	{
	if (newRegionName.length() > 0)
		{
		regionName = newRegionName;
		}
		
	else
		regionName = "None";
	}


string SalesRegion::getRegionName() //returns the region name
	{
	return regionName;
	}


void SalesRegion::processSales(string fileName) //reads a series of sales records and processes them into the seller array
	{
	ifstream inFile;
	string id; //seller's id
	double salesAmount; //sales of the transaction made

	
	cout << endl
		 << internal << setw(22) << "Sales Transactions" << endl
		 << endl
		 << left << setw(15) << "Seller ID" << "Sales Amount" <<endl
		 << endl;

	inFile.open(fileName.c_str());
	if (!inFile)
		{
		cout << "Error - unable to open input file\n";
		exit(1);
		}
	
	inFile >> id;
	while (inFile)
		{
		inFile >> salesAmount;
		
		int index = findSellerID (id); //proccesses the sales transaction to the proper element in the sellerArray
	
		if (index == - 1)
			{
			cout << left << setw (15) << id << right << setw(10) << "Error - ID not found" <<endl;
			}
							
		sellerArray[index].setSalesTotal(sellerArray[index].getSalesTotal() + salesAmount);
			 
		cout << left << setw (15) << id << right << setw(10) << salesAmount << endl;
		
		inFile >> id;
		}	 
		
	inFile.close ();
	
	cout << endl;
	}


void SalesRegion::print() //prints the seller array
	{
	cout << internal << setw(30)<< "Sales Listing for " << regionName << endl <<endl;
	
	int i;
  
	for (i = 0; i < numSellers; i++)
		{
		sellerArray[i].print();
		}

	}
	
	
	
	
int main()
	{

	SalesRegion region("Illinois", "sellersIL.txt");

	region.print();

	region.processSales("salesILJuly.txt");

	region.print();

	return 0;
	}	 
		


Make sure you #include <fstream> when working with ofstream/ifstream.

Must of your errors seem to just be caused by mislabeled variables. For example in your set functions you call them by firstName and lastName however in the actual class they are called FirstName/LastName. (capitalization counts). There are similiar things like that such as you calling a variable called sellerID in functions however it's called ID in the class.

A lot of your errors are caused because you refer to a variable called 'sellerArray'. However I don't see this declared in either of the classes so the compiler doesn't know what you are referring to.

Make sure you don't call functions/variables by the same name (eg. ID).

Carefully go through your code and make sure all of your variable names match up exactly everywhere and everything has been declared correctly. Then post whatever errors you have left.
I am not sure whether you know that the usual practice is to split up the files:

main.cpp

.h file for each class - declaration of member variables & functions
.cpp for each class - definition of member functions & initialisation of member variables

The cpp files have #include for whichever classes it needs to use.

If main() has a couple of functions, declare them before main, and put their definition after main, or put them in a header file as well.

This is better than having 300+ lines in one file.

HTH
Hi Joshua,

1
2
3
4
5
6
7
Seller::Seller(string newFirstName, string newLastName, string newID, double newSalesTotal) //contructor for seller class
	{
	setFirstName(newFirstName);
	setLastName(newLastName);
	setID (newID);
	setSalesTotal(newSalesTotal);
	}


In the above code, there is no need to call the set functions, because the constructor is a member of the class, it has access to the private variables of the class, so just assign the args of the constructor to the member variables directly.

It is when you create an object of the class type, that you don't have access to the private variables, unless you create a public get function like you have done.

Class member variables should named with m_ or m at the front of them so you know they are member variables, this avoids confusion with function args.

With the splitting of the files like I described in my last post, you should be able to set up your IDE to make the files automatically when you create a class. It should also be easy to get it to compile them all as well. If you get linker errors it probably means that a file is not being compiled along with the others. The IDE should also take care of some of the inheritance, and create constructors for you also.

Finally, it is much easier for us to help if you post the compiler output showing all the errors. We can then explain what they all mean.

Good luck!!
You declared to function with the same bane

1
2
      void setSalesTotal(double);
      double setSalesTotal();


I think that the second one shold be named as

double getSalesTotal();

In member functions of the second class you are accessing array sellerArray for example

if (sellerArray[j].getID() < sellerArray[min].getID())

but this array is not declared in the second class.
Topic archived. No new replies allowed.