![](/img/trans.png)
[英]How to fix error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'void') while using strings and stack
[英]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”)
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;
}
問題是什么?
問題是這一行: out << mobil.print() << endl;
. 您的print()
方法不返回任何內容(是void
類型),因此無法將其發送到ostream
。
為了解決這個問題,您的print()
方法應該以ostream
支持的一種類型返回您想要打印的任何內容,您可以在參考中找到。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.