0

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.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
FrostyStraw
  • 1,628
  • 3
  • 25
  • 34
  • [operator overloading](http://stackoverflow.com/questions/4421706/operator-overloading) – chris Oct 25 '13 at 02:09
  • Try `Person sum = obj1.age + obj2.age;` I think that's what you're looking for. – BrainSteel Oct 25 '13 at 02:10
  • 1
    When you write it as an member function, the code in that function executes in the context of the lhs of the expresion. `persA + persB` is equivalent to `persA.operator+(persB);`. Reading your question again, I kinda feel bad telling you this. Your post reads *I totally think I should be allowed to clean my shotgun while it's loaded* and I just handed you a shotgun. – kfsone Oct 25 '13 at 03:01
  • I would advise against this. What does it even mean, logically or semantically or whatever, to add two people together? – dreamlax Oct 25 '13 at 03:35
  • I was just trying to add their ages, and it's because I just learned about operator overloading and it was the first thing I could think of – FrostyStraw Oct 25 '13 at 04:29

3 Answers3

1

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:
    // ...
}
Community
  • 1
  • 1
Felix Glas
  • 15,065
  • 7
  • 53
  • 82
0

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.

Dominique McDonnell
  • 2,510
  • 16
  • 25
0

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.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524