having trouble with overloading operator+

This is a discussion on having trouble with overloading operator+ within the C++ Programming forums, part of the General Programming Boards category; Hi, i wanted to overload a + operator to add two objects..i have an error which states "invalid operands `employee ...

  1. #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;
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    hint: you didn't overload '+' to accept employee pointers
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  3. #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??

  4. #4
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    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.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

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

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