简体   繁体   中英

error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘void’)

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM