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.
#ifndef CCC_EMPL_H
#define CCC_EMPL_H
#include <string>
usingnamespace 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
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 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;
}
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).
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!