操作员过载无法正常工作
Operator overload not working properly
我在重载运算符时遇到了问题。
我被要求在我的代码中实现的问题:
通过向类添加正确的访问器方法(查询)并修改运算符+
以使用访问器而不是直接使用类属性,消除对类中"友元"访问权限的需求。重载运算符再次+=
作为帮助程序,因此可以执行以下操作:如果 “d”
和 “e”
是双精度变量,A
是 Account 对象。 d = e += A
;余额或A
应添加到“e”
的值中,然后返回修改后的值
我的 CPP 文件:
#include <iomanip>
#include <cstring>
#include "Account.h"
using namespace std;
Account::Account() {
name_[0] = 0;
balance_ = 0;
}
Account::Account(double balance) {
name_[0] = 0;
balance_ = balance;
}
Account::Account(const char name[], double balance) {
strncpy(name_, name, 40);
name_[40] = 0;
balance_ = balance;
}
void Account::display(bool gotoNewline)const {
cout << (name_[0] ? name_ : "No Name") << ": $" << setprecision(2) << fixed << balance_;
if (gotoNewline) cout << endl;
}
Account& Account::operator+=(const Account& other) {
balance_ += other.balance_;
return *this;
}
Account& Account::operator=(const Account& ls) {
balance_ = ls.balance_;
strncpy(name_, ls.name_, 40);
return *this;
}
Account operator+(const Account &one, const Account &two) {
return Account(one.balance_ + two.balance_);
}
std::ostream& operator<<(ostream& os, const Account& A) {
A.display(false);
return os;
}
Account& Account::operator=(const char name[]) {
strncpy(name_, name, 40);
return *this;
}
double operator+=(double& d, const Account& a)
{
d += a;
return d;
}
我的头文件:
#ifndef _ACCOUNT_H__
#define _ACCOUNT_H__
#include <iostream>
class Account {
char name_[41];
double balance_;
public:
Account();
Account(double balance);
Account(const char name[], double balance = 0.0);
void display(bool gotoNewline = true)const;
Account& operator+=(const Account& other);
Account& operator=(const Account& ls);
Account& operator=(const char name[]);
friend Account operator+(const Account &one, const Account &two);
};
std::ostream& operator<<(std::ostream& os, const Account& A);
Account operator+(const Account &one, const Account &two);
double operator+=(double& d, const Account& a);
#endif
主要。.CPP:
#include <iostream>
#include "Account.h"
using namespace std;
void displayABC(const Account& A,
const Account& B,
const Account& C) {
cout << "A: " << A << endl << "B: " << B << endl
<< "C: " << C << endl << "--------" << endl;
}
int main() {
Account A;
Account B("Saving", 10000.99);
Account C("Checking", 100.99);
Account* AC[3] = { &A, &B, &C };
double balance = 0;
displayABC(A, B, C);
A = B + C;
displayABC(A, B, C);
A = "Joint";
displayABC(A, B, C);
A = B += C;
displayABC(A, B, C);
A = B += C += 100.01;
displayABC(A, B, C);
for (int i = 0; i < 3; i++) {
cout << i + 1 << "- " << (balance += *AC[i]) << endl;
}
cout << "Total Balance: " << balance << endl;
return 0;
}
当我运行它时,它似乎在前几个输出中正常工作,但是当涉及到双运算符时,它停止工作。输出应该是:
A: Saving: $10302.98
B: Saving: $10302.98
C: Checking: $201.00
--------
1- 10302.98
2- 20605.96
3- 20806.96
Total Balance: 20806.96
但从未到达"1 - 2- 3- Total Balance:"
线。任何帮助将不胜感激。谢谢!
in
double operator+=(double& d, const Account& a)
{
d += a;
return d;
}
d += a;
在double
和Account
上运行。换句话说,double
+= Account
。这需要一个看起来像
double operator+=(double& d, const Account& a)
没错。它自称。不受控制的无限递归。最终,计算机将耗尽内存以进行进一步调用,并且会发生一些不幸的事情。
解决方案不是在函数中添加double
和Account
。反正这样做没有多大意义。
可能是一个偶然的遗漏,OP真正想做的是
double operator+=(double& d, const Account& a)
{
d += a.getBalance();
return d;
}
以添加double
和double
。
这意味着 OP 必须按照赋值规范中的建议在Account
中实现getBalance
方法:
通过向类添加正确的访问器方法(查询)
我做了一些更改。这似乎有效。
帐户标题"帐户.hpp"
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include <iostream>
class Account {
private:
char name_[41];
double balance_;
public:
Account() : balance_{0} { name_[0] = 0; }
Account(double balance) : Account() { balance_ = balance; }
Account(const char* name, double balance);
void display(std::ostream& os) const;
Account& operator+=(const Account& other);
Account& operator=(const char* name);
double get_balance() const { return balance_; }
};
std::ostream& operator<<(std::ostream& os, const Account& a);
Account operator+(const Account &one, const Account &two);
double operator+=(double& d, const Account& a);
#endif
帐户实现"帐户.cpp"
#include <iomanip>
#include <cstring>
#include "account.hpp"
using namespace std;
Account::Account(const char name[], double balance) {
strncpy(name_, name, 40);
name_[40] = 0;
balance_ = balance;
}
void Account::display(ostream& os) const {
os << (name_[0] ? name_ : "No Name") << ": $"
<< setprecision(2) << fixed << balance_;
}
Account& Account::operator+=(const Account& other) {
balance_ += other.get_balance();
return *this;
}
Account& Account::operator=(const char* name) {
strncpy(name_, name, 40);
name_[40] = 0;
return *this;
}
ostream& operator<<(ostream& os, const Account& a) {
a.display(os);
return os;
}
Account operator+(const Account &one, const Account& two) {
return Account(one.get_balance() + two.get_balance());
}
double operator+=(double& d, const Account& a) {
d += a.get_balance();
return d;
}
驱动程序代码"main.cpp"
#include <iostream>
#include "account.hpp"
using namespace std;
void displayABC(const Account& A,
const Account& B,
const Account& C) {
cout << "A: " << A << endl << "B: " << B << endl
<< "C: " << C << endl << "--------" << endl;
}
int main() {
Account A;
Account B("Saving", 10000.99);
Account C("Checking", 100.99);
Account* AC[3] = { &A, &B, &C };
double balance = 0;
displayABC(A, B, C);
A = B + C;
displayABC(A, B, C);
A = "Joint";
displayABC(A, B, C);
A = B += C;
displayABC(A, B, C);
A = B += C += 100.01;
displayABC(A, B, C);
for (int i = 0; i < 3; i++) {
cout << i + 1 << "- " << (balance += *AC[i]) << endl;
}
cout << "Total Balance: " << balance << endl;
return 0;
}
听起来你需要摆脱朋友+运算符函数,并为平衡变量添加一个访问器。我还在 +=operator(double d, const Account&a) 函数中将 d+=a 替换为 d+=a.get_balance()。
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- 为什么STD ::计数将常数传递给Lambda,而不是在弦上工作时而不是字符
- sdl软件渲染器不工作,工作在硬件加速的一个
- C++程序已停止工作-求解常微分方程