如何重载<<和>>将它们用于文件编写和类的阅读

时间:2016-07-21 09:01:39

标签: c++ file-io operator-overloading

我试图重载<<和>>类的运算符,以便它可以通过以下方式直接写入文件或从中读取。

class Record
{
public:
    int key;
    string fname;
    string lname;
    string addr;
    Record(){}
    Record(int k,string f, string l, string a):key(k),fname(f),lname(l),addr(a){}
    Record operator=(Record r2)
    {
        key=r2.key;
        fname=r2.fname;
        lname=r2.lname;
        addr=r2.addr;
    }
    friend bool operator==(const Record r1,const Record r2)
    {
        if((r1.key==r2.key)&&(r1.fname==r2.fname)&&(r1.lname==r2.lname)&&(r1.addr==r2.addr))
            return true;
        else
            return false;
    }
    friend ostream operator<<(ostream os,Record r)
    {
        os<<r.key<<" "<<r.fname<<" "<<r.lname<<" "<<r.addr<<"\n";
        return os;
    }
    friend istream operator>>(istream is,Record r)
    {
        is>>r.key>>r.fname>>r.lname>>r.addr;
        return is;  
    }
};
int main()
{
    Record r(3,"Tarun","Maganti","Hyd"),buff;    
    cout<<r;
    ifstream file1("file.txt");
    file1>>buff;
    cout<<endl<<buff;
}

当我这样做时,似乎有很多错误。我希望能够使用相同的运算符来编辑/读取控制台或文件,就像它在上述程序的主要功能中一样。

以下是错误输出:

In file included from /usr/include/c++/5/iostream:39:0,
                 from CDCv2.cpp:1: /usr/include/c++/5/ostream: In function ‘std::ostream operator<<(std::ostream, Record)’: /usr/include/c++/5/ostream:393:7: error: ‘std::basic_ostream<_CharT,
_Traits>::basic_ostream(std::basic_ostream<_CharT, _Traits>&&) [with _CharT = char; _Traits = std::char_traits<char>]’ is protected
       basic_ostream(basic_ostream&& __rhs)
       ^ CDCv2.cpp:68:10: error: within this context    return os;
          ^ In file included from /usr/include/c++/5/iostream:40:0,
                 from CDCv2.cpp:1: /usr/include/c++/5/istream: In function ‘std::istream operator>>(std::istream, Record)’: /usr/include/c++/5/istream:613:7: error: ‘std::basic_istream<_CharT,
_Traits>::basic_istream(std::basic_istream<_CharT, _Traits>&&) [with _CharT = char; _Traits = std::char_traits<char>]’ is protected
       basic_istream(basic_istream&& __rhs)
       ^ CDCv2.cpp:73:10: error: within this context    return is; 
          ^ In file included from /usr/include/c++/5/iostream:40:0,
                 from CDCv2.cpp:1: /usr/include/c++/5/istream: In function ‘int main()’: /usr/include/c++/5/istream:611:7: error: ‘std::basic_istream<_CharT, _Traits>::basic_istream(const std::basic_istream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]’ is protected
       basic_istream(const basic_istream&) = delete;
       ^ CDCv2.cpp:83:7: error: within this context    ol>>buff;
       ^ CDCv2.cpp:83:7: error: use of deleted function ‘std::basic_istream<_CharT, _Traits>::basic_istream(const std::basic_istream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]’ In file included from /usr/include/c++/5/iostream:40:0,
                 from CDCv2.cpp:1: /usr/include/c++/5/istream:611:7: note: declared here
       basic_istream(const basic_istream&) = delete;
       ^ CDCv2.cpp:70:17: note:   initializing argument 1 of ‘std::istream operator>>(std::istream, Record)’   friend istream operator>>(istream is,Record r)
                 ^ In file included from /usr/include/c++/5/iostream:40:0,
                 from CDCv2.cpp:1: /usr/include/c++/5/istream:611:7: error: ‘std::basic_istream<_CharT, _Traits>::basic_istream(const std::basic_istream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]’ is protected
       basic_istream(const basic_istream&) = delete;
       ^ CDCv2.cpp:89:7: error: within this context    ne>>buff;
       ^ CDCv2.cpp:89:7: error: use of deleted function ‘std::basic_istream<_CharT, _Traits>::basic_istream(const std::basic_istream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]’ In file included from /usr/include/c++/5/iostream:40:0,
                 from CDCv2.cpp:1: /usr/include/c++/5/istream:611:7: note: declared here
       basic_istream(const basic_istream&) = delete;
       ^ CDCv2.cpp:70:17: note:   initializing argument 1 of ‘std::istream operator>>(std::istream, Record)’   friend istream operator>>(istream is,Record r)
                 ^ CDCv2.cpp:92:2: error: a template declaration cannot appear at block scope   template <class T>   ^ CDCv2.cpp:112:11: error: ‘intersection’ was not declared in this scope for_each(intersection.begin(),intersection.end(),[](Record r){cout<<r;})
           ^ In file included from /usr/include/c++/5/iostream:39:0,
                 from CDCv2.cpp:1: /usr/include/c++/5/ostream: In lambda function: /usr/include/c++/5/ostream:391:7: error: ‘std::basic_ostream<_CharT, _Traits>::basic_ostream(const std::basic_ostream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]’ is protected
       basic_ostream(const basic_ostream&) = delete;
       ^ CDCv2.cpp:112:70: error: within this context   for_each(intersection.begin(),intersection.end(),[](Record r){cout<<r;})
                                                                      ^ CDCv2.cpp:112:70: error: use of deleted function ‘std::basic_ostream<_CharT, _Traits>::basic_ostream(const std::basic_ostream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]’ In file included from /usr/include/c++/5/iostream:39:0,
                 from CDCv2.cpp:1: /usr/include/c++/5/ostream:391:7: note: declared here
       basic_ostream(const basic_ostream&) = delete;
       ^ CDCv2.cpp:65:17: note:   initializing argument 1 of ‘std::ostream operator<<(std::ostream, Record)’   friend ostream operator<<(ostream os,Record r)

1 个答案:

答案 0 :(得分:2)

运算符operator<<operator>>使用引用返回重载,因为它们接收对象并输出包含这些对象的cout(或cin)对象。尝试:

istream& operator>>(istream& in, Record& g) {
    //do stuff here
}

它们需要通过引用返回,因为它们必须能够接收从其派生的其他流对象,例如ifstream。现在,您的程序可以读取包含Record个对象的文件。

错误的主要原因是因为basic_ostream的构造函数(iostream是其子类,cin是其中的一种类型)受到保护(这也是你的错误说)。但是你有没有看到它,因为它受到保护,它的孩子(cincout)可以被实例化?

不幸的是iostream没有operator>>operator<<功能,因此我们无法声明iostream& operator>>(iostream& iO, myType mT)。所以我们必须使用ostream类型。