2015-11-06 34 views
1

無論出於何種原因,當我嘗試從我的重載函數返回一個值時,它將返回零。我剛開始使用重載,所以我對它很陌生,所以如果這是一個簡單的錯誤,我對此感到抱歉。爲什麼我的重載操作符返回零?

這很奇怪,因爲當我在函數中的值爲cout時,它顯示了我想要看到的內容,但是當我返回時,它仍然返回一個空值,即零。

的功能是這樣的:

double operator+=(double ls, Account& rs){ 
    //cout << ls << endl; 
    //cout << rs._balance+ ls << endl; 
    return ls+=rs._balance; 



    //return rs._balance+=ls; 
} 

以下是完整的.cpp文件包括以下功能:

#define _CRT_SECURE_NO_WARNINGS 
#include <cstring> 
#include <iomanip> 
#include "Account.h" 
using namespace std; 
namespace sict{ 
    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()const{ 

     for(int x = 0; x < 40; x++){ 
      if(_name[x] == '\0') 
       x = 40; 
      else 
       cout << _name[x]; 
     } 

    cout << ": $" << setprecision(2) << fixed << _balance; 

    } 

    Account Account::operator+(Account ls) { 

     return ls._balance + _balance; 
    } 

    double operator+=(double ls, Account& rs){ 
     //cout << ls << endl; 
     //cout << rs._balance+ ls << endl; 
     return ls+=rs._balance; 



     //return rs._balance+=ls; 
    } 

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

     return _balance+=ls._balance; 

    } 

    Account::Account(const char name[]){ 
     strncpy(_name, name, 40); 
    } 

    char* Account::getName(){ 

     return _name; 
    } 

    double Account::getBal(){ 

     return _balance; 
    } 

    std::ostream& operator<<(std::ostream& ls, Account& rs){ 
     rs.display(); 
     return ls; 
    } 

    Account& Account::operator=(Account& ls){ 
     if(!strcmp(ls._name,"") &&ls._balance > 0) 
     { 
      strcpy(_name, "Saving"); 
     } 

     _balance = ls._balance; 
     //strcpy(_name, ls._name); 

     return *this; 

    } 

    char* Account::operator=(char* ls){ 

     strcpy(_name, ls); 

     return _name; 

    } 


} 

這裏是頭文件:

#ifndef SICT_ACCOUNT_H__ 
#define SICT_ACCOUNT_H__ 
#include <iostream> 
#include <cstring> 
#include <iomanip> 
namespace sict{ 
    class Account{ 
     char _name[41]; 
     double _balance; 
    public: 
     Account(); 
     Account::Account(double balance); 
     Account::Account(const char name[], double balance); 
     Account::Account(const char name[]); 
     Account& operator=(Account& ls); 
     Account operator+=(Account& ls); 
     char* operator=(char* ls); 
     void display()const; 
     double getBal(); 
     char* getName(); 

     friend double operator+=(double ls, Account& rs); 
     Account operator+(Account ls); 

    }; 

    std::ostream& operator<<(std::ostream& ls, Account& rs); 
}; 

#endif 

這裏是主要:

#include <iostream> 
#include <string> 
#include "Account.h" 

using namespace sict; 
using namespace std; 

int main() 
{ 
    Account A; 
    Account B("Saving", 10000.99); 
    Account C("Checking", 100.99); 

    double value = 0; 

    cout << A << endl << B << endl << C << endl << "--------" << endl; 

    A = B + C; 
    A = "Joint"; 

    cout << A << endl << B << endl << C << endl << "--------" << endl; 

    A = B += C; 

    cout << A << endl << B << endl << C << endl << "--------" << endl; 

    value += A; 
    value += B; 
    value += C; 

    cout << "Total balance: " << value << endl; 

    return 0; 
} 
+0

http://stackoverflow.com/questions/33550749/overloading-operator-in-c-how-do-you-pass-的重複左操作數/ 33550939#33550939 –

+0

@ASH,我googled瞭如何做到這一點,但沒有找到答案,沒想到它是一個重複,但沒關係... –

回答

2

對於

value += A; 
value += B; 
value += C; 

工作,你就必須改變重載運算符函數的參數做個參考。現在,它被傳遞了價值。該值在函數中被修改,但不會更改調用函數中的值。

而不是

double operator+=(double ls, Account& rs){ 

使用

double operator+=(double& ls, Account& rs){ 
        // ^^ 
+0

感謝您的解釋,不能相信它是如此簡單,我錯過了,非常感謝。 –

+1

@AsadMahmood,別難過。我們大多數人都經歷過這種錯誤。 –

2

爲了使這個工作,你需要通過引用捕獲左邊的參數。

double operator+=(double ls, Account& rs) 

需要是

double operator+=(double& ls, Account& rs) 
+0

工作就像一個魅力,謝謝! –

相關問題