FrostyStraw
FrostyStraw

Reputation: 1656

Trying to overload + operator

I cannot for the life of me understand why this is not working. I am so confused. I have a class Person which has a data member age, and I just want to add two people so that it adds the ages. I don't know why this is so hard, but I'm looking for examples and I feel like everyone does something different, and for some reason NONE of them work. Sometimes the examples I see have two parameters, sometimes they only have one, sometimes the parameters are references to the object, sometimes they're not, sometimes they return an int, sometimes they return a Person object. Like..what is the most normal way to do it?

class Person {
    public: 
        int age;
        //std::string haircolor = "brown";
        //std::string ID = "23432598";

        Person(): age(19) {}

        Person operator+(Person&) { }
};

Person operator+(Person &obj1, Person &obj2){
    Person sum = obj1;
    sum += obj2;
    return sum;
}

I really feel like overloading a + operator should seriously be the easiest thing in the world except I DON'T KNOW WHAT I AM DOING. I don't know if I'm supposed to create the overload function inside the class, outside, if it makes a difference, why if I do it inside it only allows one parameter, I just honestly don't get it.

Upvotes: 0

Views: 134

Answers (3)

Felix Glas
Felix Glas

Reputation: 15524

Try this:

class Person {
public: 
    int age;
//std::string haircolor = "brown";
//std::string ID = "23432598";

    Person(): age(19) {}

    Person operator+ (const Person&); // Skip the definition for later.
};

Person Person::operator+ (const Person& other) { // Declare as member of Person::
    Person sum;
    sum.age = this->age + other.age; // All member functions have pointer: this.
    return sum;
}

Or make the operator non-member:

Person operator+ (const Person& p1, const Person& p2) { // Non-member don't have pointer: this
    Person sum;
    sum.age = p1.age + p2.age;
    return sum;
}

The operator overload can be either member or non-member, it's your choice. Se this link for more info about guidelines for choosing between member and non-member operator overloads.

Note: It is common practice to declare a class' member variables as private to not expose the implementation. This will however lead to the non-member operator overload to not have access to a Person object's member age anymore. The solution (and common practice) is to add the non-member operator function as a friend to Person. Then the non-member function will have access to the private member age.

class Person {
friend Person operator+ (const Person&, const Person&);
private: 
    int age;
    // ...
public:
    // ...
}

Upvotes: 1

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275385

In Person, implement Person& operator+=(Person const& other) that increases a Person by another.

Change the signature of your free operator+ to take Person const &s instead of references.

And bob is your uncle.

Upvotes: 0

Dominique McDonnell
Dominique McDonnell

Reputation: 2520

Looks like you are trying to do both methods of operator overloading at the same time, while you only need to do one.

To fix your code, remove Person operator+(Person&) { } from Person definition.

The difference is that one is a member and one is a free function. There are advantages to both, and one or the other must be used in certain cases. In particular the member can access the private members without being declared as a friend, and operator() needs to be a member. Some operators are best implemented as a free function, for instance where you want to combine basic types like int with your class.

In this case there isn't any particular advantage of one over the other.

Upvotes: 0

Related Questions