这是重载左流操作符的正确方法吗?(C++)

0 c++ operator-overloading stream ostream

这个函数声明给了我错误:

ostream& operator<<(ostream& os, hand& obj);
Run Code Online (Sandbox Code Playgroud)

错误是:

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
Run Code Online (Sandbox Code Playgroud)

hand是我制作的一个类,display是一个类型的公共数据成员char*.

谁能告诉我我做错了什么?

Joh*_*itb 7

声明看起来正确.但错误消息表明,这ostream不是一种类型.尝试包括iostream标题,然后说std::ostream.

您应该考虑的另一件事是将参数'hand'作为const引用.所以你也可以接受临时表并打印出来.