-2

I have encountered a problem when trying to overload the '+' operator to add two Money objects rands and cents.

The error:

I encountered on the console is " 'Money Money::operator+(Money*, Money*)' must take either zero or one argument".

My resolution:

1) I tried to use the two integer values 'rands' and 'cents' in my class to store the result when adding the two money.

2) I also researched another way which was to only pass one object to the function (it seemed to work by not producing the same error, but I had to implement a getMoney() method to recieve the rands and cents individually...)

#include <iostream>

using namespace std;

class Money{
public:
    int rands;
    int cents;

    Money();
    Money(int rands,int cents);
    Money operator+(Money* obj,Money* obj2){return obj};
};

  Money::Money(){
    rands = 0;
    cents = 0;
}

  Money::Money(int randsN, int centsN){
    rands = randsN;
    cents = centsN;
}

 Money Money::operator+(Money *obj , Money *obj2){
    obj.rands += obj2.rands;
    obj.cents += obj2.cents;

    return obj;
}

int main(){
    Money totalCash=Money();

    Money cash = Money(200,20);
    Money cashTwo = Money(100,10);

    totalCash = cash + cashTwo;
}

1) FIXES: (Working code)
=========================

//Struct Money

    struct Money{
        int rands;
        int cents;

public:
    Money();
    Money(int rands,int cents);
    Money operator+(const Money&)const; // changed
};

// Operator Overload '+' : Working for adding two Money objects

     Money Money::operator+(const Money &obj)const{
         int totRands=0;
         int totCents=0;

         totRands= obj.rands + rands;
         totCents= obj.cents + cents;

         Money newMoney= Money(totRands,totCents);

        return newMoney;
    }

Thank you Stackoverflow community.

Sherenica
  • 15
  • 7
  • 1
    Why is your code using `Money*` pointers? – Eljay Mar 21 '19 at 16:51
  • https://stackoverflow.com/a/4421719/635608 – Mat Mar 21 '19 at 16:52
  • Remove obj from your argument list and replace obj in the function definition with *this . Member +operator overload takes exactly one argument. The second operand is the caller object, i.e. "this". – ozlsn Mar 21 '19 at 16:52
  • `Money Money::operator+(const Money& obj) {Money result(rand + obj.rand, cents + obj.cents); return result;}`? – David C. Rankin Mar 21 '19 at 16:56

3 Answers3

2

Your operator+ function should have the following signature if you want to use a claa member operator+:

Money operator+( const Money& ) const;

Or you may also define it as not member of the class:

Money operator+( const Money&, const Money& );
Gojita
  • 490
  • 4
  • 10
  • I tried the first declaration (it works). Although when I try the second declaration (my code yields the same error as before?). I am currently trying to understand why this is so, my build options is for C11. Might that be causing me problems? – Sherenica Mar 21 '19 at 22:11
  • @Sherenica: Where did you put the declaration ? The second declaration should be put outside the Money class. There's nothing related to C++11 here. – Gojita Mar 22 '19 at 07:50
  • I placed the declaration within the class. Thank you for the clarification. – Sherenica Mar 22 '19 at 12:25
1

The + operator can be used as a unary operator or a binary operator.

Both of them can be overloaded as either member functions or non-member functions.

When the unary opertor is overloaded a member function, it has to be of the form:

Money operator+() const;

When the unary opertor is overloaded as a non-member function, it has to be of the form:

Money operator+(Money const&);

When the binary opertor is overloaded a member function, it has to be of the form:

Money operator+(Money const& rhs) const;

When the binary opertor is overloaded as a non-member function, it has to be of the form:

Mone operator+(Money const& lhs, Money const& rhs);

Your declaration does not fit any of the above synactic forms. Hence, it is an error.

Further reading: https://en.cppreference.com/w/cpp/language/operators.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

Money::operator+ is a member function. It should be declared like:

Money operator+(const Money &obj) const;
Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
  • why the first const? –  Mar 21 '19 at 16:59
  • @jakub_d as Scott Meyers notes in [Effective C++](https://www.amazon.com/Effective-Specific-Improve-Programs-Designs/dp/0321334876), such operators should return `const` values to be consistent with built-in types, otherwise you could do things like `money1 + money2 = money3` which would be invalid with built-in types – Aykhan Hagverdili Mar 21 '19 at 17:02
  • you can't do that anyway, Money is not an lvalue; also obj should be const –  Mar 21 '19 at 17:04
  • 1
    @Ayxan I wonder if that was written in pre-C++11 days. Since C++11 that's a terrible advice, as `const` inhibits moving from the returned object (it doesn't matter in OP's case, but is important in general case). If you want to prevent `operator=` calls on rvalues, there is no need to put `const` everywhere. Simply lvalue-ref-qualify the `operator=` like so: `Money &operator=(const Money &) & = default;`! – HolyBlackCat Mar 21 '19 at 17:21
  • @jakub_d You can assign to class rvalues, unless you specifically prevent it, e.g. by lvalue-ref-qualifying `operator=`. – HolyBlackCat Mar 21 '19 at 17:22
  • @jakub_d see [this](https://wandbox.org/permlink/6EitegSKdHKn6My2) – Aykhan Hagverdili Mar 21 '19 at 17:25
  • @HolyBlackCat yes, it is pre-C++11, thanks for pointing out. I'll edit the answer accordingly – Aykhan Hagverdili Mar 21 '19 at 17:27