C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-16-2004, 03:32 AM   #1
Registered User
 
Join Date: Nov 2004
Posts: 8
having trouble with overloading operator+

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.


Code:
//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()));
}





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;
}
ryan_qut is offline   Reply With Quote
Old 11-16-2004, 05:49 AM   #2
Registered User
 
Join Date: Sep 2004
Posts: 720
hint: you didn't overload '+' to accept employee pointers
__________________
Quote:
i seem to have GCC 3.3.4
But how do i start it?
I dont have a menu for it or anything.
misplaced is offline   Reply With Quote
Old 11-16-2004, 06:22 AM   #3
Registered User
 
Join Date: Nov 2004
Posts: 8
i need more hint.

Quote:
Originally Posted by misplaced
hint: you didn't overload '+' to accept employee pointers

hi, what shd i add then??sorry for asking, but my first time writting overloading functions. i was not taught overloading for pointers, just normal + only..can give some code snipplets??
ryan_qut is offline   Reply With Quote
Old 11-16-2004, 06:43 AM   #4
chococoder
 
Join Date: Nov 2004
Posts: 497
your operator accepts employee objects, you're passing it pointers to such objects.

Either change your addition to something like total_salary = *e1 + *e2;
or change the operator to work with pointers to employees instead of employees.
You might even consider adding an operator that works on pointers so you can do both types of addition.

Your operator for pointers works just like your current one, something like
Code:
float operator+(employee* obj1, employee* obj2)
{
	return obj1->getsalary() + obj2->getsalary();
}
Mind I don't really like the idea of having an overloaded operator return something that's of a different type from the input objects.
It doesn't reflect the concept of addition.

Last edited by jwenting; 11-16-2004 at 06:46 AM.
jwenting is offline   Reply With Quote
Old 11-16-2004, 07:21 AM   #5
& the hat of GPL slaying
 
Thantos's Avatar
 
Join Date: Sep 2001
Posts: 5,732
binary + should take in two const references to your class and return a new object of that class.

Edit: Actually only the first parameter needs to be a const reference, the other can be anything else that makes sense to add to your class. The return values doesn't have to be your class but it should make sense.
So if I'm adding two employee's together I would expect an employee back since they are the same "unit". Just like if I was adding 5 miles to 10 miles I'd expect to get miles as my answer.

Last edited by Thantos; 11-16-2004 at 07:28 AM.
Thantos is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
unary operator overloading and classes coletek C++ Programming 9 01-10-2009 02:14 AM
Smart pointer class Elysia C++ Programming 63 11-03-2007 07:05 AM
Operator Overloading (Bug, or error in code?) QuietWhistler C++ Programming 2 01-25-2006 08:38 AM
operator overloading and dynamic memory program jlmac2001 C++ Programming 3 04-06-2003 11:51 PM
Overloading the [] operator trouble rmullen3 C++ Programming 2 08-01-2002 12:46 PM


All times are GMT -6. The time now is 12:11 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22