Hi,
i wanted to overload a + operator to add two objects..i have an error which states "invalid operands `employee *' and `employee *' to binary `operator +'".. i do not know what is wrong with that statement, can anyone pls point out my mistake?? Thanks. the problem lies with the overloading function but i can get it right
//overloading example
//write an overloading function so u can add the salary of 2 employee objects
#include <iostream>
#include <string>
using namespace std;
const short kmaxlength= 50;
class employee
{
protected:
char name[kmaxlength];
float salary;
public:
employee(char *emp_name, short sal);
~employee();
float getsalary();
};
//constructor
employee::employee(char *emp_name, short sal)
{
strncpy(name, emp_name, kmaxlength);
salary = sal;
cout<<"creating employee: "<<name<<endl;
}
employee::~employee()
{
cout<<"Deleting employee: "<<name<<endl;
}
float employee::getsalary()
{
return salary;
}
float operator+(employee obj1, employee obj2)
{
return((obj1.getsalary() + obj2.getsalary()));
}
//i tried it this way too and it didnt work. i m not sure if it shd be employee or float just before operator +, anybody knows??
//employee operator+(employee obj1, employee obj2)
//{
// employee temp((obj1.getsalary() + obj2.getsalary()));
// return temp;
//}
int main()
{
employee *e1;
e1 = new employee("Ryan", 1000);
employee *e2;
e2 = new employee("Jack", 500);
//Will this work instead of the above 4 lines??
//employee e1("Ryan", 1000);
//employee e2("Jack", 500);
float total_salary = 0;
total_salary = e1 + e2;
return 0;
}