Is this the correct way to overload the left-stream operator? (C++)
-
06-09-2019 - |
Question
This function declaration gives me errors:
ostream& operator<<(ostream& os, hand& obj);
The errors are:
error C2143: syntax error : missing ';' before '&'
error C4430: missing type specifier
error C2065: 'os' : undeclared identifier
error C2065: 'obj' : undeclared identifier
error C2275: 'hand' : illegal use of this type as an expression
see declaration of 'hand'
error C4430: missing type specifier
hand is a class I made, display is a public data member of type char*
.
Can anybody tell me what I'm doing wrong?
Solution
The declaration looks right. But the error message suggests that ostream
is not known as a type. Try including the iostream
header and say std::ostream
instead.
Another thing you should consider is making the parameter 'hand' a const reference. So you could also accept temporaries and print them out.
OTHER TIPS
is this inside a header? then you probably need to say std::ostream
. Make sure you #include<iosfwd>
.
Also, you can probably say const hand&
.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow