2016-03-07 50 views
0

我在编译我的程序时遇到了一个错误,我不确定如何使用相应的函数解决此问题以解决此问题。我将在下面发布Main,Header和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


答:联合:$ 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

更改+=操作方法:

Account& operator+=(const Account& other); 
+0

好的,工作!现在A值的输出是错误的。它只是“0”。00“为他们所有 – bp131313

+0

已更新上面的帖子与1错误行我现在得到 – bp131313

+1

我敢肯定,如果你使用调试器,并通过代码的步骤,你将能够弄明白。发生在A =“联合”时;被执行 –