这是重载左流运算符的正确方法是什么? (C ++)
-
06-09-2019 - |
题
这个函数的声明给了我错误:
ostream& operator<<(ostream& os, hand& obj);
的错误是:
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
手是我做了一个类,显示的类型是char*
的公共数据成员。
谁能告诉我什么,我做错了吗?
解决方案
在声明看起来右。但该错误消息表明ostream
不被称为类型。尝试包括iostream
头,说std::ostream
代替。
您应该考虑的另一件事情是使参数“手” const引用。所以,你也可以接受的临时并打印出来。
其他提示
是这样的一个标头内?那么你可能需要说std::ostream
。确保你#include<iosfwd>
。
另外,你或许可以说const hand&
。
不隶属于 StackOverflow