错误:与"operator<<"不匹配(操作数类型为"std :: ostream {aka std :: basic_ostream< char>}"和"void") [英] error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘void’)
问题描述
这是我的代码(仅写本质),我明白了:
错误:"operator<<"不匹配(操作数类型为"std :: ostream {aka std :: basic_ostream}"和"void")
Mobil类{上市:void print()const;int getNumber()const;double getData()const;朋友ostream&运算符<<(ostream& ;, const Mobil& amp;);};上游&运算符<("ostream& out,const Mobil& mobil"){出<<mobil.print()<<恩德尔退回}
出什么问题了?
问题是此行: out<<mobil.print()<<endl;
.您的 print()
方法不会返回任何内容(类型为 void
),因此无法将其发送到 ostream
.>
要解决此问题,您的 print()
方法应以 ostream
支持的一种类型返回要打印的内容,您可以在参考.
This is my code (I write the essence only) and I get this:
error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream}’ and ‘void’)
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;
}
what is the problem?
Problem is this line: out << mobil.print() << endl;
. Your print()
method doesn't return anything (is type of void
), so it can't be send to ostream
.
To solve this problem, your print()
method should return whatever you want to printout in one of types supported by ostream
which you can find in reference.
这篇关于错误:与"operator<<"不匹配(操作数类型为"std :: ostream {aka std :: basic_ostream< char>}"和"void")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!