[英]ostream::operator<< is not working with operator-> in shared_ptr
#include<fstream>
#include<string>
#include<memory>
class Log
{
private:
string errorlog;
shared_ptr<ofstream> fs;
public:
Log() :errorlog(""), fs(new ofstream("c:\\Log\\log.txt"), [](ofstream& fs) {fs.close(); })
{
}
void transferLog(string errorlog)
{
(*fs)<<(errorlog) //is working
fs->operator<<(errorlog); //not working
}
}
i know that if it works,it works well in other common situation. 我知道如果可行,在其他常见情况下也可以正常工作。
this error list 此错误列表
no instance of overloaded function "std::basic_ofstream<_Elem, _Traits>::operator<< [with _Elem=char, _Traits=std::char_traits<char>]" matches the argument list
Well don't do that then. 好吧,那就不要那样做。
An overloaded operator<<
may be defined in one of two ways. 可以通过以下两种方式之一定义重载的
operator<<
。 It may be defined as a member function, which can be called as fs->operator<<(errorlog);
它可以定义为成员函数,可以称为
fs->operator<<(errorlog);
, or it may be defined as a free-standing function, which can be called as operator<<(*fs, errorlog);
,或者可以将其定义为独立函数,可以将其称为
operator<<(*fs, errorlog);
. 。
For standard output streams, some overloads use the first form, others use the second form. 对于标准输出流,某些重载使用第一种形式,而其他重载使用第二种形式。 If you pick the wrong form, things break.
如果您选择了错误的形式,那么事情就会破裂。 Unless you have a very specific use case where you need to use one or the other, just write
*fs << errorlog;
除非您有一个非常特殊的用例,需要使用其中一个,否则只需编写
*fs << errorlog;
: that considers both forms and picks the best overload from both sets. :同时考虑两种形式,并从两种形式中选择最佳的重载形式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.