【问题标题】:error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘void’)错误:“operator<<”不匹配(操作数类型为“std::ostream {aka std::basic_ostream<char>}”和“void”)
【发布时间】:2018-11-09 19:16:51
【问题描述】:

这是我的代码(我只写精华),我明白了:

错误:'operator

class Mobil {
public:
void print() const; 
int  getNumber() const;
double getData() const;
friend ostream& operator <<(ostream&, const Mobil&);
};

ostream& operator<<(ostream& out, const Mobil& mobil) {
    out << mobil.print() << endl;
    return out;
}

有什么问题?

【问题讨论】:

  • @Rhathin 这对我来说是个完美的答案。
  • @Rhathin 不要在 cmets 部分回答,谢谢。

标签: c++ c++11


【解决方案1】:

问题是这一行:out &lt;&lt; mobil.print() &lt;&lt; endl;。您的print() 方法不返回任何内容(类型为void),因此无法发送到ostream

要解决此问题,您的 print() 方法应返回您想要打印的任何内容,该类型为 ostream 支持的类型之一,您可以在 reference 中找到该类型。

【讨论】:

  • 或者它可以使用std::ostream&amp;代表 operator&lt;&lt; 做任何需要的事情,尽管现在可以说这不那么惯用了。
猜你喜欢
  • 2021-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-18
  • 2021-08-09
  • 1970-01-01
  • 2016-12-01
相关资源
最近更新 更多