【问题标题】:Overload an Arithmetic Operator C++重载算术运算符 C++
【发布时间】:2020-03-10 13:07:09
【问题描述】:

我必须做到以下几点:

Cash.h 中,在Money 命名空间内为+ 操作的运算符重载添加一个声明;这应该需要两个 Cash 操作数并返回一个新的 Cash 值。请记住,声明将包含结果类型 (Cash)、操作名称 (operator +) 和两个参数 ((Cash a, Cash b)),后跟分号。

Cash.cpp 中,为Money 命名空间内的运算符添加定义。此定义应创建并返回一个新的 Cash 值,其 a.cents()b.cents() 的总和作为其 cents() 值。如果ab 的面额不同,您需要选择一个适合两者的面额; Cash.cpp 包含一个函数 gcf,您可以使用它来确定适合的最大值(对于此分配,您不必保证它代表实际流通的硬币)。

注意:我无法编辑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;
}

使用当前代码,我得到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&'

【问题讨论】:

    标签: c++ overloading arithmetic-expressions


    【解决方案1】:

    你现在有一个问题,一旦你解决它,你就会遇到另一个问题。

    第一个问题是

    Cash cents();
    

    这没有定义Cash 类的对象。相反,它声明了一个函数,它不接受任何参数并按值返回一个Cash 对象。

    做事

    Cash cents;
    

    定义一个对象。

    第二个问题是

    cents() = ...
    

    cents 是一个函数时,这有点道理,但它不应该是。当centsCash 对象时,您不能调用它。并且将a.cents() + b.cents()int 结果分配给Cash 对象是没有意义的。

    我想你应该设置_count 成员:

    cents._count = a.cents() + b.cents();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-11
      • 2012-07-05
      • 2010-10-19
      • 2012-10-10
      • 2010-12-10
      • 2016-07-15
      相关资源
      最近更新 更多