C++运算符<<和>>头文件中的方法,做得非常错误
我的头文件中有这段代码,并且 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);
提前致谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看起来您只需要
在这些行的前面放置一个 std 命名空间限定符,即:
Looks like you just need to do
then place a std namespace qualifier in front of them on those lines, ie:
我不清楚此代码出现的上下文,但错误表明这些声明出现在定义
ostream
和istream
之前。您是否以正确的顺序包含正确的头文件?
It's not clear to me the context this code appears in but the error suggests that these declarations appear before
ostream
andistream
are defined.Are you including the proper header files in the proper order?
您省略了包括:
注意:所有标准类型(例如
istream
、ostream
)都位于名为std
的命名空间中。因此,为了能够使用它们,您需要:std::
前缀或using namespace std;
)。对于头文件来说这是一个非常糟糕的做法,因为它可能会导致稍后包含的头文件中的命名冲突。You omitted includes:
Note: all the standard types like
istream
,ostream
live within a namespace calledstd
. So in order to be able to use them you need to either:std::
orusing 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.添加 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