重载算术运算符C++
Overload an Arithmetic Operator C++
我必须执行以下操作:
在
Cash.h
中,在Money
命名空间内为+
操作的运算符重载添加一个声明;这应该采用两个CCD_ 4操作数并返回一个新的CCD_。请记住,声明将由结果类型(Cash
(、操作名称(operator +
(和两个参数((Cash a, Cash b
((组成,后跟一个分号。在
Cash.cpp
中,在Money
命名空间中添加运算符的定义。此定义应创建并返回一个新的Cash值,该值的a.cents()
和b.cents()
的总和作为其cents()
值。如果a
和b
的面额不同,您需要选择一种与两者均匀匹配的面额;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;
}
使用此当前代码,我得到以下错误:
./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&'
您现在有一个问题,一旦解决了它,就会有另一个问题。
第一个问题是
Cash cents();
这并没有定义Cash
类的对象。相反,它声明了一个不接受参数并按值返回Cash
对象的函数。
只做
Cash cents;
以定义对象。
第二个问题是
cents() = ...
当cents
是一个函数时,这有点道理,但它不应该是。当cents
是Cash
对象时,你不能调用它。将a.cents() + b.cents()
的int
结果分配给Cash
对象也没有意义。
我想你应该设置_count
成员:
cents._count = a.cents() + b.cents();
相关文章:
- 为什么比较运算符如此快速
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 增量运算符与后缀混淆
- 一个关于在C++中重载布尔运算符的问题
- 运算符C++ "delete []"仅删除 2 个前值
- 模板类无法识别友元运算符
- 我可以使用条件运算符初始化C风格的字符串文字吗
- 关闭||运算符优化
- 通过继承类使用来自不同命名空间的运算符
- C++Cast运算符过载
- 如何使用AngelScript注册SFML Vector2运算符
- 重载元组索引运算符-C++
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 布尔比较运算符是如何在C++中工作的
- 重载运算符new[]的行为取决于析构函数
- 如何防止clang格式在流运算符调用之间添加换行符<<