使自定义类ostream可输出
Making a custom class ostream outputable?
我正试图打印支票和储蓄账户的余额。我知道你不能使用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
以您想要的方式打印您的类。
Bank::print_balances()
中不返回任何内容(也不能返回,因为它是void
函数)- 现在要做的是将
savings
和checking
(均为Account
类型)作为operator<<()
的参数传递给std::cout
- 问题是,默认情况下,
std::cout
"知道"如何只输出基元类型(如int
、float
、double
等)和一些C++标准库类型(如std::string
) -
所以,你有两个选择:
-
通过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; }
-
-
您永远不会想将金额存储在浮点变量中!(只有当你不同意每次求和或相乘时都要丢一些硬币时)
您的Account类中没有重载"<<"运算符。编译器不知道如何进行cout,应该重载Account类的插入运算符。
相关文章:
- 如何将图像传入C++可执行文件并将输出图像存储在新目录中?
- 如何在C++中编写 ostream 的向量,它接收所有不同的输出流,如 cout、ostringstream 和 ofs
- 将标准输出重定向到ostream
- 重载 ostream << 运算符,指针作为参数,导致输出上的内存地址
- 输出窗口中消息的可视化工作室源
- 附加作为空字符串的 ostream 可防止进一步追加
- CMake将依赖项复制到可执行输出路径
- 运行时 kinect 输出的 3D 可视化
- 将我的主输出库与测试可执行文件链接时出现问题
- 获取生成文件以更改输出可执行文件名称
- 如何从控制台启动可执行文件并使用Qt读取输出
- 输出可序列化的排序整数
- ostream:在 cout 或文件上输出的类
- 无法输出带有 ostream 输出C++的矢量
- UTF-8字符串上的位XOR操作可提供非UTF-8输出
- 可视化地将数组的所有元素输出到 C++ 中的文件中
- 具有浮点数的 C++ 程序的输出是可预测的吗?
- GCC 8 交叉编译器输出 ARMv7 可执行文件而不是 ARMv6
- 为什么在Shell输出中运行的可执行操作到stdout,而不是崩溃时的stderr
- 使自定义类ostream可输出