2016-07-21 7 views
-2

< <と>>演算子をオーバーロードしてファイルに書き込んだり、次のように直接読み込んだりしようとしました。<< and >をオーバーロードしてファイルの書き込みとクラスの読み込みに使用する

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

これを実行したときに、多くのエラーが発生しているようです。私は、上記のプログラムの主な機能のように、同じ演算子をコンソールやファイルから読み書きすることができるようにしたいと考えています。ここ


はエラー出力である:彼らはオブジェクトと出力にそれらを含むcout(又はcin)オブジェクトを取るため

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

ストリームをコピーするのではなく、参照を渡して返します。 – user463035818

+0

ありがとうございました。出来た。 –

+0

@ TarunMagantiソリューションは効果的でしたか? –

答えて

2

オペレータoperator<<operator>>をリターン・バイ・リファレンスを使用して、オーバーロードされオブジェクト。試してみてください:

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

彼らは、このようなifstreamとして、そこから派生する他のストリームオブジェクトに取ることができなければならないので、彼らが戻っ-で参照する必要があります。これで、あなたのプログラムはRecordオブジェクトを含むファイルを読むことができます。 (のタイプであるciniostreamはの子である、)basic_ostreamのコンストラクタは保護されているため

エラーの主な理由は、(あなたのエラーが言うこともある)です。しかし、保護されているので、その子ども(cincout)をインスタンス化することができますか?

残念ながらiostreamにはoperator>>またはoperator<<という機能がないため、iostream& operator>>(iostream& iO, myType mT)を宣言することはできません。だから我々はostreamタイプを使用しなければならない。

+0

コンパイラエラーはOPがストリームをコピーしようとしているために発生します。あなたのスニペットでそれを修正しましたが、あなたの説明が間違っています – user463035818

+0

さらに、OPの 'operator <<'と 'operator >>は実際には自由な関数です(http://en.cppreference.com/w/cpp/language/friend参照)。 ) – user463035818

+0

@ tobi303応答してくれてありがとう。私は説明を修正した。それは正しいのですか? –

関連する問題