Trouble storing class objects in a vector

Hey guys, this is my first post to the forums so please excuse the newness if I have posted something wrong. I am having trouble with trying to add class objects to a vector. I am trying to have a user be able to add, remove, clear, and display employee's from a vector but I am not sure if I am going about it correctly. I am also getting an error: expected type specifier at line 112 and I am not sure why. Thank you in advance for the help!

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 <string>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <tuple>
#include <ostream>
#include <new>

using namespace std;

class Employee
{
	string fName;
	string lName;
	string status;
	float salary;
	int age;
	int yearHired;

public:
	Employee(const string& name = "");
	string GetName();
	string GetStatus();
	float GetSalary();
	int GetAge();
	int GetYearHired();

private:
	string m_Name;
	string m_Status;
	float m_Salary;
	int m_Age;
	int m_YearHired;

};

Employee::Employee(const string& name) :
m_Name(name)
{}

string Employee::GetName()
{
	cout << "Please enter the new employee's first name: ";
	cin >> fName;
	cout << "Please enter the new employee's last name: ";
	cin >> lName;
	fName + lName = m_Name;
	return m_Name;
}

string Employee::GetStatus() 
{
	cout << "Please enter the employee's status (full time, part time, or manager): ";
	cin >> status;
	return m_Status;
}

float Employee::GetSalary()
{
	cout << "Please enter the employee's salary: ";
	cin >> salary;
	return m_Salary;
}

int Employee::GetAge() 
{
	while (true)
	{
		cout << "Please enter the employee's age: ";
		cin >> age;
		if (age > 0)
			break;
		else
			cout << "Error: Please enter a positive value.";
	}
	return m_Age;
}

int Employee::GetYearHired() 
{
	cout << "Please enter what year the employee was hired: ";
	cin >> yearHired;
	return m_YearHired;
}

class Staff
{
	vector<Employee*> emps;
	vector <Employee*>::const_iterator iter;
public:
	Staff();
	virtual ~Staff();
	void Add();
	void Remove();
	void Clear();
	void Display();
};

Staff::Staff()
{
	emps.reserve(20);
}

Staff::~Staff()
{
	Clear();
}

void Staff::Add()
{
	Employee* emp = new pEmp;
	emp->GetName();
	emp->GetStatus();
	emp->GetSalary();
	emp->GetAge();
	emp->GetYearHired();
	emps.push_back(emp);
}
Last edited on
so please excuse the newness if I have posted something wrong

The fact that you used code tags makes you a genius first-poster on this site.

Don't put local variables in the class definition! That is a bad thing to do since it makes every Employee structure that much bigger for absolutely no reason. Local variables belong in the member functions that use them. So move all of the member variables of Employee that don't start with m_ into the functions. And Staff probably shouldn't have iter (assuming it's basically a local variable, too).

It is unusual to include ostream when you've already included iostream, which is guaranteed to include both istream and ostream.

It's not usually necessary to include <new>. And you're not using <tuple>, <algorithm>, or <cstdlib>.

As for the problem on line 112, what is pEmp? Presumably it should be Employee.

This is backwards:
 
	fName + lName = m_Name;


As you have no main, so I can't actually run your program.
Last edited on
Thank you for the heads up on the local variables, I really did not know how to go about storing all of the class objects into the vector, I found an example that declared the vector in a new class and used pointers to the employee class.
As for the pEmp, I was trying to create a new pointer to the employee class. I am not sure why I was getting the error message though. I have just gotten stuck at this point, I was trying to get the Employee class objects to store in a vector so that I can add and remove new employees; and then be able to display or clear the vector of employees.
Topic archived. No new replies allowed.