Trouble with program using vectors

Hey guys. I wrote this program for an assignment involving vectors. I'm very new to vectors but I think I got it down. The code compiles fine until I try to display the vector. I'm getting a "no operator found which takes a right=hand operand of type 'Employee;'

Not sure what the problem is. Anybody know how to fix this or what I'm doing wrong? Any help is appreciated.

This is my course 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
#include <iostream>
#include <string>
#include <vector>
#include "ccc_empl.h"

using namespace std;

int main()
{
	
	int numOfEmploy;
	string name;
	double salary;
	

	cout << "Please enter the number of employees: ";
	cin >> numOfEmploy;

	vector<Employee> staff(numOfEmploy);

	for (int i = 0; i < numOfEmploy; i++)
	{
		cout << "Please enter Employee name: ";
		cin >> name;
		cout << "Please enter Employee salary: ";
		cin >> salary;
		staff[i] = Employee(name,salary);
	}

	for (int i = 0; i < staff.size(); i++)
	{
		cout << staff[i];
	}

	Employee emp1;
	int last = staff.size() - 1;
	int middle = staff.size()/2;

	staff.push_back(staff[last]);
	for(int i = last; i > middle; i--)
		staff[i] = staff[i - 1];
	staff[middle] = emp1;

	staff.pop_back();

	for (int i = 2; i < staff.size() - 1; i++)
	{
		staff[i] = staff[i + 1];
	}
	staff.pop_back();

	for (int i = 0; i < staff.size(); i++)
	{
		cout << staff[i];
	}



	cout << "Please press enter once or twice to continue...";
	cin.ignore().get();    			// hold console window open
	return EXIT_SUCCESS;           	// successful termination
    
}


and this is my header file:
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
#ifndef CCC_EMPL_H
#define CCC_EMPL_H

#include <string>

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;
private:
   string name;
   double salary;
};

#endif 


and this is my implementation:
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
#include "ccc_empl.h"

Employee::Employee()
{  
   salary = 0;
}

Employee::Employee(string employee_name, double initial_salary)
{  
   name = employee_name;
   salary = initial_salary;
}

void Employee::set_salary(double new_salary)
{  
   salary = new_salary;
}

double Employee::get_salary() const
{  
   return salary;
}

string Employee::get_name() const
{  
   return name;
}


Thanks again in advance for any help.
52
53
54
55
for (int i = 0; i < staff.size(); i++)
{
    cout << staff[i];
}

How does cout know how to print out an Employee object?

It doesn't, so you'll have to write the code to do it yourself.
The only thing is, I can't alter the header or the implementation files. My professor provided them to us and told us not to alter them. How would I go about writing that if I can't change those files?
You don't need to change those files at all.

You can do it in two different ways: either write the code in the loop, or write a separate operator<< function for it.

Option 1 might look something like this:
52
53
54
55
56
for (int i = 0; i < staff.size(); i++)
{
    cout << "Name: " << staff[i].get_name() << '\n';
    cout << "Salary: " << staff[i].get_salary() << '\n';
}

and Option 2 might look something like
1
2
3
4
5
6
7
// (Put this somewhere after your includes but before main() )
ostream& operator<<(ostream& os, const Employee& e)
{
    os << "Name: " << e.get_name() << '\n';
    os << "Salary: " << e.get_salary() << '\n';
    return os;
}
(then leave your for loop untouched).
I had just realized that just now! So i got the program to build, but when i debug it, I'm able to enter the amount of elements in the vector, and enter all information, but when I'm done with the last one, the program just runs through it really fast and then quits without prompting. Any idea why that would happen?

I was having trouble trying to figure out how to access the middle of a vector of user inputted size. I wrote staff.size()/2 but i'm sure that's wrong. Any idea how to access the middle of a vector? my professor wants us to add a new Employee object in the middle of the vector.
I tested the code in your first post and I'm not getting the behavior you describe. Could you perhaps post your current main function?

To insert an element in the middle of the vector, you're going to need an iterator to the position right after the spot you want to insert it to.
So staff.insert(staff.begin() + staff.size() / 2, someEmployeeObject) should work (hopefully).
Last edited on
Thanks for testing it for me, I appreciate that. It was just a fluke I guess cause it didn't happen to me after a couple of tries. I'll give that a try, hopefully it'll work. It runs well right now. Our professor only lectured us for an hour on vectors so she should catch us up with any questions next class. Thanks again for the help!
Topic archived. No new replies allowed.