操作员超载无法正常工作

时间:2016-03-13 03:29:03

标签: c++ debugging

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

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

通过向类添加适当的访问器方法(查询)并修改运算符+以使用访问器而不是直接使用类属性,消除了对类中“朋友”访问权限的需要。 重载操作符+=再次作为帮助程序,以便可以执行以下操作: 如果“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;
    }

我的HEADER档案:

#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

MAIN.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:"行。任何帮助是极大的赞赏。谢谢!

2 个答案:

答案 0 :(得分:0)

我做了一些改变。这似乎有效。

帐户标题“account.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

帐户实施“account.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;
}

听起来你需要摆脱朋友+操作员功能并为平衡变量添加一个访问器。我还在+ =运算符(double d,const Account&amp; a)函数中用d + = a.get_balance()替换了d + = a。

答案 1 :(得分:0)

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必须按照作业规范中的建议在getBalance中实施Account方法:

  

通过向类

添加适当的访问器方法(查询)