Reputation: 8143
I am using fstream and got some error.
Here is what I have:
class CLog
{
void printOn(std::ostream& dbg) const;
}
void operator>>(const CLog& s, std::ofstream& dbg)
{
s.printOn(dbg);
}
But when I compile I got the following error:
error C2664: 'printOn' : cannot convert parameter 1 from
'class std::basic_ofstream<char,struct std::char_traits<char> >' to
'class std::basic_ostream<char,struct std::char_traits<char> > &'
A reference that is not to 'const' cannot be bound to a non-lvalue
I thought that ofstream inherit from ostream so why it is not possible?
Thanks
Upvotes: 1
Views: 418
Reputation: 5693
Make printOn public and include fstream header :).
#include <fstream>
class CLog
{
public:
void printOn(std::ostream& dbg) const
{
}
};
std::ofstream & operator<<( std::ofstream& dbg, const CLog& s)
{
s.printOn(dbg);
}
Upvotes: 2
Reputation: 52294
I'd suggest posting complete code allowing to reproduce the problem. I've none with:
#include <fstream>
class CLog
{
public:
void printOn(std::ostream& dbg) const;
};
void operator>>(const CLog& s, std::ofstream& dbg)
{
s.printOn(dbg);
}
Upvotes: 1
Reputation: 23624
More correct declaration of output operator is following:
std::ostream& operator << (std::ostream& dbg, const CLog& s)
{
s.printOn(dbg);
return dbg;
}
Upvotes: 6