RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 645383
Accepted
pank
pank
Asked:2020-03-29 01:33:27 +0000 UTC2020-03-29 01:33:27 +0000 UTC 2020-03-29 01:33:27 +0000 UTC

尝试输出 endl 时出错:'operator<<' 不匹配(操作数类型为 'A' 和 '<未解析的重载函数类型>')

  • 772

我想编写一个包装类,根据变量,将或不将日志输出到stdout和/或log.txt.

#include <iostream>
#include <fstream>
using namespace std;

struct A {
  bool use_log = 1, use_filelog = 0;
  ofstream log_file;
  void open() {
    use_filelog = 1;
    log_file.open("log.txt");
  }
};

template <class T> A &operator<<(A &a, T x) {
  if (a.use_log) {
    std::cout << x;
  }
  if (a.use_filelog) {
    a.log_file << x;
  }
  return a;
}

int main() {
  A a;
  a << "Hello, World!" << endl; // <= ERROR
}

当我尝试显示时出现错误endl。我不明白这是什么问题。

这个类可以用其他方式实现吗?

这是输出画布的开头gcc:

4.cpp: In function ‘int main()’:
4.cpp:26:24: error: no match for ‘operator<<’ (operand types are ‘A’ and ‘<unresolved overloaded function type>’)
   a << "Hello, World!" << endl;
                        ^
4.cpp:14:23: note: candidate: template<class T> A& operator<<(A&, T)
 template <class T> A &operator<<(A &a, T x) {
                       ^
4.cpp:14:23: note:   template argument deduction/substitution failed:
4.cpp:26:27: note:   couldn't deduce template parameter ‘T’
   a << "Hello, World!" << endl;
                           ^
In file included from /usr/include/c++/5/iostream:39:0,
                 from 4.cpp:1:
/usr/include/c++/5/ostream:628:5: note: candidate: template<class _CharT, class _Traits, class _Tp> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&)
     operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)

但是来自clang:

prog.cpp:26:27: error: reference to overloaded function could not be resolved; did you mean to call it?
  a << "Hello, World!" << endl;
                          ^~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/ostream:590:5: note: possible target for call
    endl(basic_ostream<_CharT, _Traits>& __os)
    ^
prog.cpp:14:23: note: candidate template ignored: couldn't infer template argument 'T'
template <class T> A &operator<<(A &a, T x) {
c++
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    AnT stands with Russia
    2020-03-29T01:43:51Z2020-03-29T01:43:51Z

    std::endl没有特定的类型,因为它本身就是一个函数模板。因此,表格的记录

    a << endl;
    

    是不明确的,不可能从中导出模板参数。编译器告诉你什么。要消除歧义,您必须明确指定至少一个模板的参数。

    例如,如果您显式指定模板参数std::endl

    a << std::endl<char, std::char_traits<char>>;
    

    然后你的类型被T成功推断出来,你的操作员将按预期工作。

    但是为了不在现实中这样变态,可以类比标准流,再创建一个专门为输出设计的输出语句std::endl

    A &operator <<(A &a, 
                   std::basic_ostream<char>& (*func)(std::basic_ostream<char>&))
    {
      if (a.use_log) {
        std::cout << func;
      }
      if (a.use_filelog) {
        a.log_file << func;
      }      
      return a;
    }
    

    同时,这个算子的实现,在模板参数std::endl已经固定后,不能显式写出来,而是委托给你的模板算子(这里需要注意不要安排递归)

    A &operator <<(A &a, 
                   std::basic_ostream<char>& (*func)(std::basic_ostream<char>&))
    {
      return operator << <>(a, func);
    }
    

    为了支持其余的操纵器,您必须从链接中实施组 10-12 的其余运算符。

    • 3

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    Python 3.6 - 安装 MySQL (Windows)

    • 1 个回答
  • Marko Smith

    C++ 编写程序“计算单个岛屿”。填充一个二维数组 12x12 0 和 1

    • 2 个回答
  • Marko Smith

    返回指针的函数

    • 1 个回答
  • Marko Smith

    我使用 django 管理面板添加图像,但它没有显示

    • 1 个回答
  • Marko Smith

    这些条目是什么意思,它们的完整等效项是什么样的

    • 2 个回答
  • Marko Smith

    浏览器仍然缓存文件数据

    • 1 个回答
  • Marko Smith

    在 Excel VBA 中激活工作表的问题

    • 3 个回答
  • Marko Smith

    为什么内置类型中包含复数而小数不包含?

    • 2 个回答
  • Marko Smith

    获得唯一途径

    • 3 个回答
  • Marko Smith

    告诉我一个像幻灯片一样创建滚动的库

    • 1 个回答
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Алексей Шиманский 如何以及通过什么方式来查找 Javascript 代码中的错误? 2020-08-03 00:21:37 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5