C++运算符的链接<<对于 std::cout 之类的用法

发布于 2024-12-19 18:18:03 字数 1036 浏览 1 评论 0 原文

可能的重复:
重载运算符时 std::endl 的类型未知<<
运算符重载

我目前正在编写一个记录器类,但是operator<<方法会导致编译器错误。这是该类的最小化版本,位于文件“logger.h”中:

#include <iostream>
class Logger {
public:
    Logger() : m_file(std::cout) {}

    template <typename T>
    Logger &operator<<(const T &a) {
        m_file<<a;
        return *this;
    }

protected:
    std::ostream& m_file;
};

它包含在我的 main.cpp 中,并且当我输出字符串文字时可以正常工作:

log << "hi"; 

但是,以下内容将无法编译。

#include "logger.h"
int main() {
    Logger log;

    log << std::endl;
}

g++ 编译器报告:

src/main.cpp:5:错误:与“operator<<”不匹配在'日志<< std::endl'

Possible Duplicate:
std::endl is of unknown type when overloading operator<<
Operator overloading

I'm currently programming a logger class, but the operator<< method causes a compiler error. Here's a minimized version of the class, in file "logger.h":

#include <iostream>
class Logger {
public:
    Logger() : m_file(std::cout) {}

    template <typename T>
    Logger &operator<<(const T &a) {
        m_file<<a;
        return *this;
    }

protected:
    std::ostream& m_file;
};

It is included in my main.cpp and works perfecly when I output a string literal:

log << "hi"; 

However, the following won't compile.

#include "logger.h"
int main() {
    Logger log;

    log << std::endl;
}

The g++ compiler reports:

src/main.cpp:5: error: no match for 'operator<<' in 'log << std::endl'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无法回应 2024-12-26 18:18:03

您的问题不是关于 << 链,而是单个 log << endl 也会导致问题。这是因为 std::endl 是一个模板函数:

template <class charT, class traits>
basic_ostream<charT,traits>& endl(basic_ostream<charT,traits>& os);

basic_ostreamoperator<< 的重载之一是:

template <class charT, class traits = char_traits<charT> >
class basic_ostream : virtual public basic_ios<charT,traits> {
public:
    basic_ostream<charT,traits>& operator<<(
    basic_ostream<charT,traits>& (*pf)(basic_ostream<charT,traits>&));
//...
};

所以模板参数当使用std::cout<时可以推导出来。但是,当左侧是class Logger时,编译无法推导出endl的模板参数。显式给出模板参数可以让程序编译并运行:

#include <iostream>
class Logger
{
public:
    std::ostream &m_file;
    Logger(std::ostream &o = std::cout):m_file(o){};

    template <typename T>
    Logger &operator<<(const T &a) {
        m_file<<a;
        return *this;
    }
};

int main()
{
    Logger log;
    log<<std::endl<char, std::char_traits<char> >;
    log<<"hi"<<" stackoverflow"<<std::endl<char, std::char_traits<char> >;
    return 0;
}

或者可以在class Logger中添加新的operator<<重载,让编译器推导出<的模板参数code>std::endl:

#include <iostream>
class Logger
{
public:
    std::ostream &m_file;
    Logger(std::ostream &o = std::cout):m_file(o){};

    template <typename T>
    Logger &operator<<(const T &a) {
        m_file<<a;
        return *this;
    }

    Logger &operator<<(std::ostream& (*pf) (std::ostream&)){
        m_file<<pf;
        return *this;
    }
};

int main()
{
    Logger log;
    log<<std::endl;
    log<<"hi"<<" stackoverflow"<<std::endl;
    return 0;
}

另外,如果不需要立即刷新输出,可以使用 '\n' 代替 endl

Your problem is not about the chain of << , a single log << endl would also cause the problem. It is because std::endl is a template function:

template <class charT, class traits>
basic_ostream<charT,traits>& endl(basic_ostream<charT,traits>& os);

One of the overload of operator<< in basic_ostream is:

template <class charT, class traits = char_traits<charT> >
class basic_ostream : virtual public basic_ios<charT,traits> {
public:
    basic_ostream<charT,traits>& operator<<(
    basic_ostream<charT,traits>& (*pf)(basic_ostream<charT,traits>&));
//...
};

So the template parameters can be deduced when std::cout<<std::endl is used. However, when the left side is the class Logger, the compile cannot deduce the template parameters of endl. Explicitly give the template parameters can let program compile and work:

#include <iostream>
class Logger
{
public:
    std::ostream &m_file;
    Logger(std::ostream &o = std::cout):m_file(o){};

    template <typename T>
    Logger &operator<<(const T &a) {
        m_file<<a;
        return *this;
    }
};

int main()
{
    Logger log;
    log<<std::endl<char, std::char_traits<char> >;
    log<<"hi"<<" stackoverflow"<<std::endl<char, std::char_traits<char> >;
    return 0;
}

Or you can add a new overload of operator<< in class Logger to let compiler deduce the template parameters of std::endl:

#include <iostream>
class Logger
{
public:
    std::ostream &m_file;
    Logger(std::ostream &o = std::cout):m_file(o){};

    template <typename T>
    Logger &operator<<(const T &a) {
        m_file<<a;
        return *this;
    }

    Logger &operator<<(std::ostream& (*pf) (std::ostream&)){
        m_file<<pf;
        return *this;
    }
};

int main()
{
    Logger log;
    log<<std::endl;
    log<<"hi"<<" stackoverflow"<<std::endl;
    return 0;
}

Also, if you don't need the output to be flushed immediately, you can use '\n' instead of endl.

只有一腔孤勇 2024-12-26 18:18:03

该错误是由函数 std::endl 引起的。参考:

重载运算符时std::endl的类型未知<< ;

The error is caused by std::endl which is a function. Refer to:

std::endl is of unknown type when overloading operator<<

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文