-4

I have to do the following:

In Cash.h, add a declaration inside the Money namespace for an operator overload for the + operation; this should take two Cash operands and return a new Cash value. Remember that the declaration will consist of the result type (Cash), the operation name (operator +), and two arguments ((Cash a, Cash b)), followed by a semi-colon.

In Cash.cpp, add a definition for the operator inside the Money namespace. This definition should create and return a new Cash value which has the total of a.cents() and b.cents() as its cents() value. If the denominations of a and b are different, you will need to choose a denomination that fits evenly into both; Cash.cpp contains a function, gcf, which you can use to determine the largest value that fits (for this assignment, you do not have to guarantee that it represents an actual circulated coin).

Note: I can't edit solution.cpp

//Cash.cpp

#include "Cash.h"
#include <cstdlib>
#include <utility>
#include <iostream>

    int gcf(int a, int b)
    {
        using std::swap;

        if (abs(a) < abs(b)) { swap(a, b); }
        if (b == 0) { return a; }
        a %= b;
        return a == 0? b: gcf(b, a);
    }

    namespace Money {
            Cash::Cash(int d, int c)
            : _count{c}, denomination{d} 
        {
            if (_count < 0 || denomination <= 0) {
                std::cerr << "Cash object constructed with invalid currency count.\n";
            }
        }

        // only code I can edit in this file, rest is locked
        const Cash operator + (const Cash& a, const Cash& b){
          Cash cents();
          cents() = a.cents() + b.cents();
          return cents();
        };    
    }
//Cash.h

#pragma once
#ifndef MONEY_CASH
#define MONEY_CASH

    namespace Money {
        class Cash {
        public:
            Cash(): Cash{1, 0} {}
            Cash(int d, int c);
            Cash(const Cash& other): Cash{other.denomination, other._count} {}

            int count() const { return _count; }
            int cents() const { return _count * denomination; }

            const int denomination;
        private:
            int _count;
        };

        // only code I can edit in this file, rest is locked
        const Cash operator + (const Cash& a, const Cash& b);
    }

    #endif
//Solution.cpp

#include <iostream>
#include "Cash.h"

int main(int argc, char* argv[])
{
    using namespace std;
    using namespace Money;

    int D, N;
    cin >> D >> N;
    Cash a {D, N};
    cin >> D >> N;
    Cash b {D, N};
    Cash c = a + b;
    cout << "Result: " << c.cents() << " cents in " << c.denomination << "s.";
    return 0;
}

With this current code I get the following error:

./Cash.cpp: In function 'const Money::Cash Money::operator+(const Money::Cash&, const Money::Cash&)':
./Cash.cpp:28:39: error: no match for 'operator=' (operand types are 'Money::Cash' and 'int')
         cents() = a.cents() + b.cents();
                                       ^
In file included from ./Cash.cpp:1:
./Cash.h:7:11: note: candidate: 'Money::Cash& Money::Cash::operator=(const Money::Cash&)' <deleted>
     class Cash {
           ^~~~
./Cash.h:7:11: note:   no known conversion for argument 1 from 'int' to 'const Money::Cash&'
Holt
  • 36,600
  • 7
  • 92
  • 139

1 Answers1

2

You have one problem now, and once you fix it you will have another.

The first problem is

Cash cents();

This doesn't define an object of the Cash class. Instead it declares a function that takes no arguments and returns a Cash object by value.

Just do

Cash cents;

to define an object.

The second problem is

cents() = ...

It kind of made sense when cents was a function, but it shouldn't be. When cents is a Cash object then you can't call it. And it makes no sense to assign the int result of a.cents() + b.cents() to a Cash object.

I suppose you should set the _count member:

cents._count = a.cents() + b.cents();
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621