简体   繁体   English

iostream的C ++编译错误

[英]C++ compile error with iostream

I am using fstream and got some error. 我正在使用fstream并出现一些错误。

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? 我以为ofstream从ostream继承而来,那为什么不可能呢?

Thanks 谢谢

More correct declaration of output operator is following: 输出运算符的更正确声明如下:

std::ostream& operator << (std::ostream& dbg, const CLog& s)
{
    s.printOn(dbg);
    return dbg;
}

Make printOn public and include fstream header :). 将printOn公开,并包括fstream标头:)。

#include <fstream>
class CLog
{
public:
    void printOn(std::ostream& dbg) const
    {

    }
};

std::ofstream & operator<<( std::ofstream& dbg, const CLog& s)
{
    s.printOn(dbg);
}

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM