Struggling with a C2143 syntax error

I am struggling with the above error in visual studio 2010. I know the code works but V/S keeps throwing the error. I am very new in C++ and V/S. Can anyone out the help? I am getting the error on lines 109, 143 and 209. Thank you

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
 #include<iostream>
#include<fstream>
#include<istream>
#include<ostream>
#include<string>
#include<conio.h>
#include<vector>
#include<string>
#include<algorithm>

using namespace std;

/**
A basic employee class that is used in many examples
in the book "Computing Concepts with C++ Essentials"
*/
class Employee {
public:
	/**
	Constructs an employee with empty name and no salary.
	*/
	Employee();
	/**
	Constructs an employee with a given name and salary.
	@param employee_name the employee name
	@param initial_salary the initial salary
	*/
	Employee(string employee_name, double initial_salary);

	/**
	Sets the salary of this employee.
	@param new_salary the new salary value
	*/
	void set_salary(double new_salary);

	/**
	Gets the salary of this employee.
	@return the current salary
	*/
	double get_salary() const;

	/**
	Gets the name of this employee.
	@return the employee name
	*/
	string get_name() const;
	void set_name(string newName);
private:
	string name;
	double salary;
};

/**
Constructs an employee with empty name and no salary.
*/
Employee::Employee() {
	Employee::name = "New Employee";
	Employee::salary = 0;
}
/**
Constructs an employee with a given name and salary.
@param employee_name the employee name
@param initial_salary the initial salary
*/
Employee::Employee(string employee_name, double initial_salary) {
	Employee::name = employee_name;
	Employee::salary = initial_salary;
}

/**
Sets the salary of this employee.
@param new_salary the new salary value
*/
void Employee::set_salary(double new_salary) {
	Employee::salary = new_salary;
}

/**
Gets the salary of this employee.
@return the current salary
*/
double Employee::get_salary() const {
	return Employee::salary;
}

/**
Gets the name of this employee.
@return the employee name
*/
string Employee::get_name() const {
	return Employee::name;
}

void Employee::set_name(string newName) {
	Employee::name = newName;
}

// declares the length of each record
const int NEWLINE_LENGTH=2;
const int RECORD_SIZE=30+10+NEWLINE_LENGTH;
std::vector<Employee> employees; // stores collection of Employee objects 
string filename; // file to read and write

int findEmployee(string empName) {
	int empPos = 0;

	// search the Employee collection for the employee
	
	for(Employee emp : employees)
	{
		if(emp.get_name() == empName) {
			return empPos;
		}
		empPos++;
	};
	return -1;
}

void deleteEmployee() {
	string empToFind;
	cout << "Enter the name of the employee: ";
	cin >> empToFind;

	int empPos = findEmployee(empToFind);

	if(empPos == -1) {
		// employee doesn't exist, so don't try deleting
		cout << "\nEmployee '" << empToFind << "' was not found.\n";
	} else {
		// null out the employee.
		employees[empPos].set_name("");
		employees[empPos].set_salary(0);
		cout << "\nEmployee '" << empToFind << "' was deleted.\n";
	}
}

void listEmployees() {
	// output employee list headings
	cout << "\nName\t\tSalary";
	cout << "\n------\t\t------";

	// output employees
	for(Employee emp : employees)
	{
		cout << "\n" << emp.get_name();
		cout << "\t\t" << emp.get_salary();
	}
	cout << "\n";
}

bool addEmployee() {
	string empName;
	double empSalary;

	cout << "\nEnter a name for the employee: ";
	cin >> empName;

	cout << "\nEnter a salary for the employee: ";
	cin >> empSalary;

	// find out if employee already exists
	if(findEmployee(empName) != -1) {
		cout << "\nEmployee already exists.";
		return false; 
	}

	int emptyEmpPos = findEmployee("");

	if(emptyEmpPos != -1) {
		// add the new employee to one of the empty spots if one exists
		employees[emptyEmpPos].set_name(empName);
		employees[emptyEmpPos].set_salary(empSalary);
	} else {
		// add a new entry in the employees collection
		employees.push_back(Employee(empName, empSalary));
	}

	return true;
}

bool loadEmployeesFromFile(string filename) {
	fstream fs;

	// opens the file for reading and writing
	fs.open(filename.c_str());

	// open the file for input.
	ifstream infile(filename, std::ifstream::in); // create the stream in read-only mode
	if(!infile) {
		cout << "Cannot open file for reading.\n";
		_getch();
		return false;
	} 

	// load all of the employees from the file into an Employee vector collection
	string empName;
	double salary;	
	while(infile >> empName >> salary) {
		employees.push_back(Employee(empName, salary));
	}
	fs.close();

	return true;
}

void writeEmployeesToFile() {
	std::ofstream outfile (filename,std::ios::out);

	for(Employee emp : employees) 
	{
		// don't write the deleted employees to file
		if(emp.get_name() != "") {
			outfile << emp.get_name() << "\t\t" << emp.get_salary() << "\n";
		}
	}
	outfile.close();
}

// main function
int main()
{
	cout << "Please enter the data file: ";	
	int selectedOption = 3;

	// reads the filename from the user
	cin >> filename;

	// attempt to read file and load employees
	if(loadEmployeesFromFile(filename) == false) {
		return 1;
	}


	while (selectedOption != 0)
	{
		cout << "\nOptions: \n[1] Add Employee \n[2] Delete Employee \n[3] List Employees \n[0] Exit \n\nPlease select option: ";
		cin >> selectedOption;

		if(selectedOption == 0) { // exit
			// write employees to file first
			writeEmployeesToFile();
			// exit program
			return 0;
		} else if(selectedOption == 1) { // add employee
			addEmployee();
		} else if(selectedOption == 2) { // delete employee
			deleteEmployee();
		} else { // default to list employees
			listEmployees();
		}
	}

	return 1; // if we return here then something went wrong
}
Last edited on
Well quite clearly all the errors are occurring on the range for loops. I don't actually know much about the loops myself but this still looks right to me.
Perhaps you'll just have to compensate and either compile this little bit outside of VS or replace it for the standard for loop.
Hi G8rCre8or,

I think you might benefit from a bit of reorganisation to the design. As well as the Employee class there could a class which holds multiple employees. The vector you have on line 100 would be a private member variable of the EmployeeContainer class (you can use your own meaningful name, I just used this one because it explains what it holds), and this avoids it being a global variable - which is itself a bad idea. Then you would define an interface with public functions that allow you to interact with your vector of employees. This interface is all of the functions outside the Employee class you have already, just that they will be member functions of the EmployeeContainer class.

There is a concept called encapsulation, which means in part that all the functions & data to do with an object should be part of that object's class. If you do things how I described above, then you won't be breaking this rule.

I would be careful about making local objects in functions, better to create them in main() , and use their interface functions from there.

Another point of reorganisation is to have your class declarations in their own header files, and their implementations of their functions in a corresponding .cpp file. So have Employee.h , Employee.cpp , EmployeeContainer.h , EmployeeContainer.cpp and main.cpp . This is just a much better way to organise things.

Hope all goes well.

Edit:

Also, there is no need to have a get / set function for each member variable of a class. Get functions are reasonably benign (and are often abused), but set functions are particularly bad nearly all the time. Often it is possible to get rid of all of them.

First, you can have constructors with initialiser lists.
www.cprogramming.com/tutorial/initialization-lists-c++.html‎


Next, instead of doing this:

212
213
214
if(emp.get_name() != "") {
			outfile << emp.get_name() << "\t\t" << emp.get_salary() << "\n";
		}


Have a member function in the Employee class which does the same thing. This is also part of the encapsulation rule. This way you might be able to replace several or all of the get functions with 1 function that does output.

If you need to change the value of data members after they have been constructed: consider having one function which gets the input from the user, validates it, and then sets all the data members in one go, rather than a function for each and every data member.
Last edited on
All very good advice IdeasMan, but doesn't help his/her problem.

I also can't compile similar code to OP in VS 2010. And i missing an include or something??
I dumbed it right down and just did this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<vector>

int main()
{

	std::vector<int> intVector;

	intVector.push_back(1);
	intVector.push_back(5);
	intVector.push_back(12);
	intVector.push_back(17);

	for(auto myint : intVector)
	{

	}

	return 0;
}


And get the same compilation error. I don't use ranged c++11 for loops in my code at work, but am curious to know what's going on. I might try it on VS 2012 tonight.
Visual C++ 2010 doesn't support range based for loops, so line 13 in your dumbed down code won't work.

But they are supported by Visual C++ 2012 and later.

Andy

PS Visual C++ 2010 does support this non-standard syntax ("inherited" from C++/CLI)

1
2
3
4
5
6
	// etc

	for each(auto myint in intVector) // quasi C# loop syntax
	{

	// etc 
Last edited on
ah right. thank you :)
I guess that answers the OP's questions as well.
Topic archived. No new replies allowed.