使自定义类ostream可输出

Making a custom class ostream outputable?

本文关键字:可输出 ostream 自定义      更新时间:2023-10-16

我正试图打印支票和储蓄账户的余额。我知道你不能使用void函数返回一个值,但我如何显示两个账户的余额?

#ifndef ACCOUNT_H
#define ACCOUNT_H
// Account.h
// 4/8/14
// description
class Account {
private:
    double balance;
    double interest_rate; // for example, interest_rate = 6 means 6%
public:
    Account();
    Account(double);
    void deposit(double);
    bool withdraw(double); // returns true if there was enough money, otherwise false
    double query();
    void set_interest_rate(double rate);
    double get_interest_rate();
    void add_interest();
};
#endif

// Bank.cpp
// 4/12/14
// description
#include <iostream>
#include <string>
#include "Bank.h"
using namespace std;
Bank::Bank(): checking(0), savings(0) { }
Bank::Bank(double checking_amount, double savings_amount): checking(checking_amount), savings(savings_amount){;
    checking = Account(checking_amount);
    savings = Account(savings_amount);
}
void Bank::deposit(double amount, string account)
{
    if (account == "S") {
        savings.deposit(amount);
    } if (account == "C") {
        checking.deposit(amount);
    }
}
void Bank::withdraw(double amount, string account)
{
    if (account == "S") {
        savings.withdraw(amount);
    } if (account == "C") {
        checking.withdraw(amount);
    }
}
void Bank::transfer(double amount, string account)
{
    if (account == "S") {
        savings.deposit(amount);
        checking.withdraw(amount);
    } if (account == "C") {
        checking.deposit(amount);
        savings.withdraw(amount);
    }
}
void Bank::print_balances()
{
    cout << savings << endl;
    cout << checking << endl;
}
#ifndef BANK_H
#define BANK_H
// Bank.h
// 4/12/14
// description
#include <string>
#include "Account.h"
using namespace std;
class Bank {
private:
    Account checking;
    Account savings;
public:
    Bank();
    Bank(double savings_amount, double checking_amount);
    void deposit(double amount, string account);
    void withdraw(double amount, string account);
    void transfer(double amount, string account);
    void print_balances();
};
#endif

我在void Bank::print_balances()下得到两个错误。它只是说:

"no match for 'operator<<' in 'std::cout << ((Bank*)this) ->Bank::savings'"

我读了很多关于它的书,但我所了解到的是,由于"支票"answers"储蓄"是一种账户类型,所以它不起作用。我以前的项目类似,我有"double"类型,所以我可以返回一个值。

如果格式有误,很抱歉。第一次在这个网站上发帖。

您需要为自定义类Account重载operator<<,以便能够cout<<的对象。

class Account {
    ...
public:
    friend ostream& operator<<(ostream &os, const Account &dt);
    ...
};
ostream& operator<<(ostream &os, const Account &dt)
{
    os << dt.balance; // print its balance
    return os;
}

要继续阅读,请检查Overloading the I/O operators。

检查此页:重载<lt;您自己班级的接线员。它说明了如何让cout以您想要的方式打印您的类。

  1. Bank::print_balances()中不返回任何内容(也不能返回,因为它是void函数)
  2. 现在要做的是将savingschecking(均为Account类型)作为operator<<()的参数传递给std::cout
  3. 问题是,默认情况下,std::cout"知道"如何只输出基元类型(如intfloatdouble等)和一些C++标准库类型(如std::string
  4. 所以,你有两个选择:

    • 通过getter从Account对象获得的输出基元类型:

      class Account {
            ...
            double GetBalance() const { return balance; }
            ...
      };
      void Bank::print_balances()
      {
           std::cout << savings.GetBalance();
           ...
      
    • 或者通过重载operator<<()来告诉std::cout如何打印Account对象(如其他答案中所述)。

      std::ostream& operator<<(std::ostream& os, const Account& a)
      {
          os << a.GetBalance();
          return os;
      }
      
  5. 您永远不会想将金额存储在浮点变量中!(只有当你不同意每次求和或相乘时都要丢一些硬币时)

您的Account类中没有重载"<<"运算符。编译器不知道如何进行cout,应该重载Account类的插入运算符。