C++运算符<<和>>头文件中的方法,做得非常错误

发布于 2024-11-02 07:29:36 字数 476 浏览 0 评论 0 原文

我的头文件中有这段代码,并且 ostream 和 istream 行上有很多错误。一个错误是“&”之前“缺少”;”,我很困惑,我对此很陌生,抱歉

#pragma once

class ArrayIntStorage
{
public:
    void readFromFile();
    bool setReadSort(bool);
    void sortStd();
    void sortOwn();

    ArrayIntStorage(void);
    ~ArrayIntStorage(void);
};  

ostream& operator<< (ostream &out, const ArrayIntStorage &a);
istream& operator>> (istream &in, ArrayIntStorage &b);

提前致谢

I have this code in my header file and Ive got loads of errors on the ostream and istream lines. One error is "missing ";" before "&"" and im confuzzled, im new to this sorry

#pragma once

class ArrayIntStorage
{
public:
    void readFromFile();
    bool setReadSort(bool);
    void sortStd();
    void sortOwn();

    ArrayIntStorage(void);
    ~ArrayIntStorage(void);
};  

ostream& operator<< (ostream &out, const ArrayIntStorage &a);
istream& operator>> (istream &in, ArrayIntStorage &b);

thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

挽梦忆笙歌 2024-11-09 07:29:36

看起来您只需要

    #include <ostream>
    #include <istream>

在这些行的前面放置一个 std 命名空间限定符,即:

    std::ostream& operator<<(std::ostream& out,...)

Looks like you just need to do

    #include <ostream>
    #include <istream>

then place a std namespace qualifier in front of them on those lines, ie:

    std::ostream& operator<<(std::ostream& out,...)
油焖大侠 2024-11-09 07:29:36

我不清楚此代码出现的上下文,但错误表明这些声明出现在定义 ostreamistream 之前。

您是否以正确的顺序包含正确的头文件?

It's not clear to me the context this code appears in but the error suggests that these declarations appear before ostream and istream are defined.

Are you including the proper header files in the proper order?

向日葵 2024-11-09 07:29:36

您省略了包括:

#include <istream>
#include <ostream>

注意:所有标准类型(例如 istreamostream)都位于名为 std 的命名空间中。因此,为了能够使用它们,您需要:

  • 在它们前面加上 std:: 前缀或
  • 使用命名空间(using namespace std;)。对于头文件来说这是一个非常糟糕的做法,因为它可能会导致稍后包含的头文件中的命名冲突。

You omitted includes:

#include <istream>
#include <ostream>

Note: all the standard types like istream, ostream live within a namespace called std. So in order to be able to use them you need to either:

  • prefix them with std:: or
  • use the namespace (using namespace std;). This is a very bad practice for a header file as it may cause naming clashes in the header files that are included later on.
纸短情长 2024-11-09 07:29:36

添加 iostream include 并将方法原型放入类中并将其声明为友元。我无法提供更多详细信息,因为我是通过手机回复的。

这是一个链接:Operator-Overloading/Classlevelostreamoperatorandistreamoperator.htm">http://www.java2s.com/Tutorial/Cpp/0200_Operator-Overloading/Classlevelostreamoperatorandistreamoperator.htm

Add the iostream include and Put the method prototype inside the class and declare it friend. I can't give more details since I am replying from my mobile.

Here is a link: Operator-Overloading/Classlevelostreamoperatorandistreamoperator.htm">http://www.java2s.com/Tutorial/Cpp/0200_Operator-Overloading/Classlevelostreamoperatorandistreamoperator.htm

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文