![]() |
| | #1 |
| Registered User Join Date: Nov 2004
Posts: 8
| having trouble with overloading operator+ 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 | |
| | #2 | |
| Registered User Join Date: Sep 2004
Posts: 720
| hint: you didn't overload '+' to accept employee pointers
__________________ Quote:
| |
| misplaced is offline | |
| | #3 | |
| Registered User Join Date: Nov 2004
Posts: 8
| i need more hint. Quote:
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 | |
| | #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();
}
It doesn't reflect the concept of addition. Last edited by jwenting; 11-16-2004 at 06:46 AM. |
| jwenting is offline | |
| | #5 |
| & the hat of GPL slaying 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |