0

我正在尝试通过使用和编写自己的流操纵器来获取 adouble并将其作为货币输出。iomanip我通过编写一个返回 ostream 并将 ostream 作为参数(以及两个整数参数,宽度和精度)的方法来做到这一点

std::ostream& Currency(std::ostream& os, int width, int precision)
{
    os << "$";
    os << std::setprecision(precision);
    os << std::right;
    os << std::setw(width);
    os << std::fixed << std::setfill('0');
    return os;
}

$0000128.00但是,我得到的不是我期望的输出,而是输出$0x7ffeb45783d8128.00

我只是这样称呼它:

cout << Currency(cout, 11, 2) << balance << endl;

不知道问题是什么,这似乎与它被包装在一个函数中有关。如果我将不在函数中的确切代码复制到我的实际输出中,那么它可以完美运行。

同样在我在天平上运行货币操纵器之前,我还运行:

out << std::right << std::setfill('0') << std::setw(10) << a.accountNo << " ";
out << std::setfill(' ') << std::left << std::setw(19) << a.name << " ";
out << std::setfill(' ') << std::left << std::setw(3) << a.sex << " ";
out << std::setfill(' ') << std::left << std::setw(10) << a.dob.toString() << " ";
out << std::setfill(' ') << std::left << std::setw(40) << a.address << " ";
4

1 回答 1

1
cout << Currency(cout, 11, 2) << balance << endl;

执行该Currency函数后,您最终会得到类似于以下内容的内容:

cout << cout << balance << endl;

打印没有重载,ostream但是有一个转换为void*打算在布尔上下文中使用的转换,该上下文启动并生成您得到的打印输出

于 2015-05-26T09:24:36.837 回答