【问题标题】:Is this the correct way to overload the left-stream operator? (C++)这是重载左流运算符的正确方法吗? (C++)
【发布时间】:2010-10-30 00:58:54
【问题描述】:

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

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

hand 是我做的一个类,display 是 char* 类型的公共数据成员。

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

【问题讨论】:

    标签: c++ operator-overloading stream ostream


    【解决方案1】:

    声明看起来正确。但是错误消息表明ostream 不是一种类型。尝试包含 iostream 标头并改为使用 std::ostream

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

    【讨论】:

      【解决方案2】:

      这是在标题中吗?那么你可能需要说std::ostream。确保你#include&lt;iosfwd&gt;

      另外,你也可以说const hand&amp;

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-28
        • 2017-03-24
        相关资源
        最近更新 更多