【发布时间】:2014-12-14 10:16:57
【问题描述】:
我已经实现了一个日志类 TLogFile,现在我想重载输出操作符
TLogFile* log = new TLogFile("some arguments...");
*log << "Hello world."; // (1)
*log << "Hello world." << endl; // (2)
*log << std::hex << setw(2) << setfill('0') << someValue << endl; // (3)
我使用 ostream 作为班级成员和朋友。该类如下所示:
namespace app {
class TLogFile
{
public:
app::TLogFile& operator<< (std::string& out);
std::ostream& operator<< (std::ostream& out);
friend std::ostream& operator<< (std::ostream& out, TLogFile& o);
};
} // namespace app
只有纯文本 (1) 使用字符串版本。我一使用 endl (2) 或 iomanip (3) 就会收到错误消息:
../src/main.cpp:164:70: 错误:'sysdat.app::cSystemData::obj.app::cSystemObjects::applicationLog->app:: 中的 'operatorsysdat.obj.applicationLog (const std::allocator*)(& std::allocator())))))
我相信其中一个 ostream 版本应该可以工作。 有谁知道如何重载运算符以便可以使用 endl 和 iomanip?
【问题讨论】:
-
std::endl不是字符串或流。