制作自定义 cout 时出错
error while making custom cout
当我尝试运行不处理链接(out<<"one"<<"two")的代码版本时,我正在尝试创建一个自定义 cout 类,该类将文本输出到控制台输出和日志文件,它工作正常,但是当我尝试让它处理链接时,它给了我"这个运算符函数的参数太多"。我错过了什么?
class CustomOut
{
ofstream of;
public:
CustomOut()
{
of.open("d:\NIDSLog.txt", ios::ate | ios::app);
}
~CustomOut()
{
of.close();
}
CustomOut operator<<(CustomOut& me, string msg)
{
of<<msg;
cout<<msg;
return this;
}};
你需要一个成员operator<<
,它返回对对象实例的引用:
class CustomOut
{
...
CustomOut& operator<<(string const& msg)
{
// Process message.
f(msg);
return *this;
}
};
这将允许您以链接方式"流式传输"到CustomOut
类中:
CustomOut out;
out << str_0 << str_i << ... << str_n;
相关文章:
- 访问者访问变体并返回不同类型时出错
- 在Linux for Windows上编译C++代码时出错
- 读取文件的最后一行并输入到链接列表时出错
- 重载操作程序时出错>>用于类中的字符串 memebr
- 调用专用模板时出错"no matching function for call to [...]"
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- LINK 尝试使用 OpenSSL evp aes 256 c++ 时出错
- std::cout.imbue()多重调用
- 在Google Kick start中提交时出错
- 在c++中访问int到类对象的映射时出错
- 分段错误当我试图运行程序时出错
- 使用dynamic_cast和构造函数时出错
- CHECK(调用)函数在Google Colab中出错
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 在C++中将cout与变量一起使用时出错
- 制作自定义 cout 时出错
- C++:将自定义哈希键值从 unordered_map 输出到 std::cout 时出错
- 在可视化cpp控制台应用程序中使用std::cout出错
- 尝试使用cout在循环中打印数组时出错