操作员过载无法正常工作

Operator overload not working properly

本文关键字:工作 常工作 操作员      更新时间:2023-10-16

我在重载运算符时遇到了问题。

我被要求在我的代码中实现的问题:

通过向类添加正确的访问器方法(查询)并修改运算符+以使用访问器而不是直接使用类属性,消除对类中"友元"访问权限的需求。重载运算符再次+=作为帮助程序,因此可以执行以下操作:如果 “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;doubleAccount上运行。换句话说,double += Account 。这需要一个看起来像

double operator+=(double& d, const Account& a)

没错。它自称。不受控制的无限递归。最终,计算机将耗尽内存以进行进一步调用,并且会发生一些不幸的事情。

解决方案不是在函数中添加doubleAccount。反正这样做没有多大意义。

可能是一个偶然的遗漏,OP真正想做的是

double operator+=(double& d, const Account& a)
{
    d += a.getBalance();
    return d;
}

以添加doubledouble

这意味着 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()。