2017-06-28 25 views
4

したがって、std::ostream_iteratorstd::iostream_iteratorを使用して、ファイルからの書き込みと読み取りを試みています。 writngのプロセスは間違いなくうまく動作します。しかし、読書については、私は失われています。私が持っているエラーは、次のとおりです。バイナリオーバーロード演算子=

1> C:\プログラムファイル(x86の)マイクロソフトのVisual Studio 14.0 \ VCの\ \ xutility(2316)\含ま:エラーC2678:バイナリ '=':なしオペレータは見つかりませんでした'constのWRstruct' タイプの左側のオペランドを取ります(あるいは全く許容変換が存在しない)

は、それが言うこと:

C:\ Users \ユーザーXXXXXXX \デスクトップ\ TTTTT \ TTTTT \ wrstruct.h(21):注: 'WRstruct &' WRstruct :: operator =(const WRstruct &) ' 1> C:注意:マイクロソフトのVisual Studio \ \プログラムファイル(x86の)14.0 \ VCの\は\ xutility(2316)などが何(のconst WRstruct、WRstruct) '引数リストに一致する

をしようとしたときに、 operator=をオーバーロードする正しい方法はありますか?

クラス:

#include <iostream> 
#include <string> 
#include <fstream> 
#include <vector> 
#include <iterator> 
#include <istream> 

class WRstruct 
{ 
private: 
    std::string name; 
    std::string number; 
    friend std::ostream& operator<<(std::ostream&, const WRstruct&); 
    friend std::istream& operator >> (std::istream& is, WRstruct&); 

public: 
    WRstruct(){}; 
    void write(); 
    void read(); 
    ~WRstruct(){}; 
}; 
std::ostream& operator<<(std::ostream& os, const WRstruct& p) 
{ 
    os << "User Name: " << p.name << std::endl 
     << "Name: " << p.number << std::endl 
     << std::endl; 
    return os; 
} 

std::istream& operator >> (std::istream& is, WRstruct& p) 
{ 

    is >> p.name>>p.number; 
    return is; 
} 

方法:

void WRstruct::write() 
{ 
    std::vector<WRstruct> vecP; 
    std::copy(std::istream_iterator<WRstruct>(std::cin), 
    std::istream_iterator<WRstruct>(), std::back_inserter(vecP)); 
    std::ofstream temp("temp.txt", std::ios::out); 
    std::ostream_iterator<WRstruct>temp_itr(temp, "\n"); 
    std::copy(vecP.begin(), vecP.end(), temp_itr); 

} 

void WRstruct::read() 
{ 
    std::vector<WRstruct> vec; 

    std::ifstream readFile("temp.txt"); 
    std::istream_iterator<WRstruct> istr(readFile); 
    copy(vec.begin(), vec.end(), istr); 

    std::istream_iterator<WRstruct> end_istr; 
    copy(istr, end_istr, back_inserter(vec)); 

    std::ostream_iterator<WRstruct> osIter(std::cout," "); 
    copy(vec.begin(),vec.end(),osIter); 

} 

とメイン():私の知る限り理解し

#include <iostream> 
#include "WRstruct.h" 

int main() 
{ 
    WRstruct r; 
    r.write(); 
    //r.read(); 

    return 0; 
} 
+0

あなたの問題とは無関係ですが、構造体の入力演算子は、出力演算子が書き込むものを読み取ることができません。また、 'static'または' inline'のマークを付けるべきです。そうしないと、複数のソースファイル([*翻訳単位*](https://en.wikipedia.org/)からクラスを使用しようとすると問題が発生します。 wiki/Translation_unit_(プログラミング)))。また、いくつかの[ヘッダーを含むガード](https://en.wikipedia.org/wiki/Include_guard)が必要です。 –

+2

'copy(vec.begin()、vec.end()、istr);行にコメントしてください。この行では、 'vector'(空)から' istream'(読み取り専用)にデータをコピーしようとしています。非常に奇妙に見えます。多分このミスはこの行にあります。 – alexeykuzmin0

+0

@ alexeykuzmin0ああ、ベクトルが空ではない、それは 'std :: cin'から上に配置されているはずです。 –

答えて

2

、あなたの関数WRstruct::readの意味は " 'temp.txt'からすべてのデータを読み込み、それをconsoに書き込むle "である。 ところで、関数readが何かを出力するのは奇妙なので、それに応じて関数の名前を付けることを検討してください。

istream_iteratorを使用してファイルから何かを読み取るには、イテレータのペア(ファイルの先頭に1つのポインティング、および別の空)を作成し、std::copyを使用する必要があります。だから、あなたの関数の読取部だから

std::vector<WRstruct> vec; 

std::ifstream readFile("temp.txt"); 
std::istream_iterator<WRstruct> istr(readFile); 
std::istream_iterator<WRstruct> end_istr; 
copy(istr, end_istr, back_inserter(vec)); 

のようになります、あなただけのコンパイルエラーを取り除くためにWRstruct::readから1行をコメントまたは削除することができます。

+0

'WRstruct :: read'関数の意味が異なるはずです。 – alexeykuzmin0

+0

私はすべての変更後に別のエラーがあることを示すためにコードを編集できますか?それらのうちの1つは次のとおりです: 'エラーLNK2005:" class std :: basic_ostream >&__cdecl operator <<(class std :: basic_ostream >&、クラスWRstruct Source.objに既に定義されている 1> C:\ Users \のように定義されています(const 6 @ YAAAV?$ basic_ostream @ DU?$ char_traits @ D @ std @@@ std @@ AAV01 @ ABVWRstruct @ Nikita Gusev \ Desktop \ ttttt \ Debug \ ttttt.exe:致命的なエラーLNK1169:複数の定義済みシンボルが見つかりました。それとも別の質問をしますか? – X21

+1

@ X21、別の質問を作成する方が良いと思います。この質問のコードを使用して、私はこの2番目のエラーメッセージを再現できません – alexeykuzmin0