开发者

C++ operator<< and >> methods in the header file, done very wrong

开发者 https://www.devze.com 2023-02-26 18:42 出处:网络
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

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);

开发者_StackOverflowthanks in advance


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,...)


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?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消