### 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:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263`` ``````#include #include #include #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 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:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445`` ``````#ifndef CCC_EMPL_H #define CCC_EMPL_H #include 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:
 ``123456789101112131415161718192021222324252627`` ``````#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.
 ``52535455`` ``````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:
 ``5253545556`` ``````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
 ``1234567`` ``````// (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.