Problems with vectors

Pages: 12
Sup guys it's me again. My problem this time is i cant figure out how to make a vector full of objects and pass it to a function.

Here is what this part of the program should do: Create a vector to hold objects of class Manager. Pass this vector to a function that will prompt the user for input for various variables. Create a new Manager object from the user input, push back the vector, and store the object in the new spot the vector just made.

Here is what im trying to do in main.cpp..(lots of code was removed because it was really long and no one wants to read long code)
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
	vector<Employee*> database(0);

			switch( selection2)
			{
			case 'a': // Add manager;
				AddManager(database);
			}// end switch 2
	cin.get();
	return 0;
}


and here is my function ins Manager.cpp...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void	Manager::AddManager(vector<employee>& database)
{
	cout << "First Name : ";
	std::string firstName;
	cin >> firstName;
	cout << "Last Name : ";
	std::string lastName;
	cin >> lastName;
	cout << "Salary : ";
	int salary;
	cin >> salary;
	cout << "Meetings per Week : ";
	int meetingsPerWeek;
	cin >> meetingsPerWeek;
	cout << "Vacations Days per Year : ";
	int vacationDaysPerYear;
	cin >> vacationDaysPerYear;
	database.push_back;
	database.back = new Manager(firstName, lastName, salary, 
				meetingsPerWeek, vacationDaysPerYear);
}


Errors im getting:
>employee in AddManager(vector<employee>& database) says...
 
Error:employee is undefined

^^I included "Employee.h" which contains the employee class

>The dots in database.push_back and database.back say...
 
Error: a pointer to a bound function may only be used to call the function.
The class is called Employee, not employee.
The task description is nonsense. push_back does not reserve space for another element, it adds an element to the vector.
So it should be database.push_back(new Manager(...));
Oh thanks dude. Here is my new code...

1
2
database.push_back(new Manager(firstName, lastName, salary, 
							meetingsPerWeek, vacationDaysPerYear));


But now the dot says...

Error: no instance of overloaded function "std::vector<_Ty,_Ax>::push_back[with _ty=Employee, _A=std::allocator<Employee>]" matches argument list

Well, you have a vector of Employee objects, but you're trying to add a pointer to the vector.
Choose one.
I just want the vector to hold Employee objects :(.

How would i do it without pointers?
If you want to store the objects directly, don't create them using new, i.e. database.push_back(Manager(...));
However, that won't work with inheritance, so your vector would need to store Manager objects. If you want it to be any type of Employee, you'll have to stick with pointers.
okay thanks then how do i do it with pointers?

EDIT: i think i fixed it but now in main it says AddManager(database*) is udefined. Im pretty sure its the parameter but i dont know how to fix it.

the original function is this

AddManager(vector<Employee*>& database)
Last edited on
the original function is this
AddManager(vector<Employee*>& database)

If that is the case, then it certainly does not fit with this line:
void Manager::AddManager(vector<Employee*>& database)
I dont get it they're the same
No. One is a free function, one is a member function of the Manager class.
OH no they're the same i just wanted to show you the original parameter stuff.

wait... does AddManager have to be used as

x.AddManager()?

If it has to be used that way then how would i make it work without using it that way?
Okay, i just skipped the Function and put all the code in main.

now new is underlined red and says...

Error: conversion to inaccessible base class "Employee" is not allowed

I know what it means i just dont know how to fix it.

 
database.push_back(new Manager(firstName, lastName, salary, meetingsPerWeek, vacationDaysPerYear));
wait... does AddManager have to be used as
x.AddManager()?

Yes, unless you make it static. Then you could use Manager::AddManager().

As for the error, you're probably using private inheritance instead of public.
UGH when i make it public switch is underlined and it says...
 
Error: transfer of control bypasses initialization of:
Heh. You shouldn't declare variables in the scope of a switch case.
If you need any, give the case its own block, so the variables are destroyed at the end.

1
2
3
4
5
6
7
8
switch(bla)
{
  case 'a':
  {
    int some,variables;
    //...
  }
}
So then i would need to use a function?

If i make it a method of Manager then it says its undefined in main.

:(
So then i would need to use a function?

For?

If i make it a method of Manager then it says its undefined in main.

What is "it"? Some code would be nice.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cout << "First Name : ";
	std::string firstName;
	cin >> firstName;
	cout << "Last Name : ";
	std::string lastName;
	cin >> lastName;
	cout << "Salary : ";
	int salary;
	cin >> salary;
	cout << "Meetings per Week : ";
	int meetingsPerWeek;
	cin >> meetingsPerWeek;
	cout << "Vacations Days per Year : ";
	int vacationDaysPerYear;
	cin >> vacationDaysPerYear;
	database.push_back(new Manager(firstName, lastName, salary, 
									meetingsPerWeek, vacationDaysPerYear))


The code im talking about is right there^^ and also in the OP.
And what is the error message? Which line does it point to?
There is no error message. The code is not done, its just that some stuff is underlined red so i know it's going to make an error later.

switch is underlined and says "Error:transfer of control bypasses initilization of:"

and AddManager is underlined and says its undefined.

the AddManager function is a method of the Manager class and looks like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void	Manager::AddManager(vector<Employee*> database)
{
	cout << "First Name : ";
	std::string firstName;
	cin >> firstName;
	cout << "Last Name : ";
	std::string lastName;
	cin >> lastName;
	cout << "Salary : ";
	int salary;
	cin >> salary;
	cout << "Meetings per Week : ";
	int meetingsPerWeek;
	cin >> meetingsPerWeek;
	cout << "Vacations Days per Year : ";
	int vacationDaysPerYear;
	cin >> vacationDaysPerYear;
	database.push_back(new Manager(firstName, lastName, salary, 
									meetingsPerWeek, vacationDaysPerYear));
}
Last edited on
Pages: 12