c++ - 如何诊断流式运营商问题 : no match for 'operator<<'

标签 c++ compiler-errors

关闭。这个问题需要debugging details .它目前不接受答案。












想改进这个问题?将问题更新为 on-topic对于堆栈溢出。

1年前关闭。




Improve this question




我真的不想诉诸于此,但我真的不知道为什么我的代码没有编译(gcc)..('模板参数推导/替换失败'等)
错误信息:

error: no match for 'operator<<' (operand types are 'std::basic_ostream<char>' and 'const EAN_code')
头文件
#ifndef HEADER_H
#define HEADER_H
using namespace std;
#include <iostream>

class EAN_code {
  int ean_code;
  
};

class Order_item {
  EAN_code item_number;
  unsigned qty;
public:
  Order_item(const EAN_code&, unsigned);
  ostream& print(ostream&) const;
};

ostream& operator<<(ostream&, const Order_item);

#endif
源文件
#include "header.h"


Order_item::Order_item (const EAN_code& code, unsigned qty) : item_number{code}, qty{qty} {}

ostream& Order_item::print(ostream& o) const {
  return o << qty << " x " << item_number;
}
ostream& operator<<(ostream& o, const Order_item i) {
  return i.print(o);
}

最佳答案

问题在这里:

return o << qty << " x " << item_number;
具体来说:
<< item_number
item_number 的类型为 EAN_code而您还没有提供 operator<<为它重载。使用与您已经为 Order_item 所做的类似的模式
我建议您将问题标题更改为“如何查找 C++ 流操作符错误”之类的内容。

关于c++ - 如何诊断流式运营商问题 : no match for 'operator<<' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63728479/

相关文章:

c++ - 错误 : ‘father’ is an inaccessible base of ‘son’

c++ - 如何捕获 substr 异常?

c++ - 指向多态类层次结构中派生类型的智能指针,编译错误

c# - (ILC0005错误)我无法编译UWP x64/x86发布项目

c++ - 从模板类切换时,为什么出现<错误类型>?

c++ - 为什么我的 .cpp 文件没有被处理?

c++ - 如何使用 Boost.Asio 解析 IPv4 地址的真实广播地址

c++ - glBuffer 使用一种数据样式而不使用另一种样式,为什么?

C 错误 : braced-group within expression allowed only inside a function

ios - Apple LLVM 编译器 4.2 错误退出代码 1