[Problem] Operator Overloading +

Write your question here.
Dear C++ Community

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...)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  Put the code you need help with here.

#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)
=========================
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//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 C++ community.
Last edited on
> Money operator+(Money* obj,Money* obj2){return obj};
You can't just make up interfaces, you have to follow pre-defined recipes.
https://en.cppreference.com/w/cpp/language/operators
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <iomanip>

struct Money {

    int rands ;
    int cents ;

    void normalise() {

        if( rands < 0 ) rands = 0 ;
        if( cents < 0 ) cents = 0 ;

        const auto total_cents = rands*100 + cents ;
        rands = total_cents / 100 ;
        cents = total_cents % 100 ;
    }

    explicit Money( int rands = 0, int cents = 0 ) : rands(rands), cents(cents) {

        normalise() ;
    }

    // see: https://en.cppreference.com/w/cpp/language/operators#Binary_arithmetic_operators
    //  Since for every binary arithmetic operator there exists a corresponding compound assignment operator,
    // canonical forms of binary operators are implemented in terms of their compound assignments
    Money& operator += ( const Money& that ) {

        rands += that.rands ;
        cents += that.cents ;
        normalise() ;
        return *this ;
    }

    friend Money operator+ ( Money a, const Money& b ) { return a += b ; }

    // see: https://en.cppreference.com/w/cpp/language/operators#Stream_extraction_and_insertion
    friend std::ostream& operator<< ( std::ostream& stm, const Money& m ) {

        return stm << "ZAR " << m.rands << '.'
                   << std::setw(2) << std::setfill('0') << m.cents ;
    }
};

int main() {

    Money a( 23, 78 ) ;
    Money b( 36, 40 ) ;

    std::cout << a << " + " << b << " == " << a+b << '\n' ;
}

http://coliru.stacked-crooked.com/a/93e0b83b46390b30
Thank you. I really appreciate all your help.
Registered users can post here. Sign in or register to post.