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;
}

Recommended Answers

All 3 Replies

sorry, i thought they were two different forums...pls ignore this thread then.

DaniWeb and cprogramming.com are indeed two entirely different forums and communities. Jwenting is just pointing out that your question has already been completely answered at that other site. So members here should not take the time to reply the same answers you've already received at the other site.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.