重载运算符无法正常工作?

时间:2016-03-07 02:09:56

标签: c++ debugging operator-overloading overloading

我在编译程序时遇到错误,并且不确定如何使用适当的函数来解决此问题。我将发布下面的主要,标题和CPP文件,其中包含所需的结果。任何帮助/提示表示赞赏,谢谢!

我得到错误:

Error   C2679   binary '+=': no operator found which takes a right-hand operand of type 'double'

更新: 我已修复代码以更新“=”运算符重载,除了1行输出外它正在工作。

这是我得到的输出:

答:没有名字:$ 0.00

B:保存:$ 10000.99

C:检查:100.99美元

答:没有名字:$ 10101.98

B:保存:$ 10000.99

C:检查:100.99美元

A:联合:$ 0.00

B:保存:$ 10000.99

C:检查:100.99美元

答:储蓄:$ 10101.98

B:节省:$ 10101.98

C:检查:100.99美元

答:储蓄:$ 10302.98

B:储蓄:$ 10302.98

C:检查:$ 201.00

出于某种原因,“JOINT”余额为0,我不确定为什么。它应显示为$ 10101.98

主要:

#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);
    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);
    return 0;
}

源文件:

#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+=(Account& other) {

            balance_ += other.balance_;
            return *this;
    }

    Account& Account::operator=(const Account& ls) {

                    strcpy(name_, ls.name_);

            balance_ = ls.balance_;

            return *this;
     }
     Account operator+(const Account &one, const Account &two) {
            return Account(one.balance_ + two.balance_);
    }

    ostream& operator<<(ostream& os, const Account& A) {
            A.display();
            return os;
    }

标题文件:

#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+=(Account& other);
            Account& operator=(const Account & other);
            friend Account operator+(const Account &one, const Account    &two);  



    };

    std::ostream& operator<<(std::ostream& os, const Account& A);
};

#endif

1 个答案:

答案 0 :(得分:1)

+=运算符方法更改为:

Account& operator+=(const Account& other);
相关问题