So I'm trying to write and read from a file, using std::ostream_iterator
and std::iostream_iterator
. The process of writng works well without any mistakes. But as for reading I'm lost. The error, I have is:
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility(2316): error C2678: binary '=': no operator found which takes a left-hand operand of type 'const WRstruct' (or there is no acceptable conversion)
and it says that:
c:\users\xxxxxxx\desktop\ttttt\ttttt\wrstruct.h(21): note: could be 'WRstruct &WRstruct::operator =(const WRstruct &)' 1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility(2316): note: while trying to match the argument list '(const WRstruct, WRstruct)'
What is the correct way of overloading operator=
?
class:
#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;
}
Methods:
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);
std::ifstream readFile("temp.txt");
std::istream_iterator<WRstruct> istr(readFile);
std::istream_iterator<WRstruct> end_istr;
copy(vecP.begin(), vecP.end(), istr);
copy(istr, end_istr, back_inserter(vecP));
std::ostream_iterator<WRstruct> osIter(std::cout, " ");
copy(vecP.begin(), vecP.end(), osIter);
}
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);
}
and main():
#include <iostream>
#include "WRstruct.h"
int main()
{
WRstruct r;
r.write();
//r.read();
return 0;
}
Aucun commentaire:
Enregistrer un commentaire