Answer the question
In order to leave comments, you need to log in
How to solve c++ stream iterator problem?
Now I understand with STL. I always use stream iterators and the copy algorithm for input and output. But when deriving structural objects, studio 12 notes an error (the constructor is either not available or declared explicit). Even my prog teacher didn't give me an answer to this question.
A simple example:
#include
#include
#include
#include
#include
#include
#include
using namespace std;
class Student
{
private:
string name;
intgroup;
double mark;
public:
Student ()
{ }
Student(Student& a)
{
name=a.name;
group=a.group;
mark=a.mark;
}
Student(string name1,int group1,double mark1)
{
name=name1;
group=group1;
mark=mark1;
}
~Student()
{ }
friend ostream& operator<<(ostream& s,Student& a)
{
s<>(istream& s,Student& a)
{
s>>a.name;
s>>a.group;
s>>a.mark;
return s;
}
Student& operator=(Student& b)
{
name=b.name;
group=b.group;
mark=b.mark;
return *this;
}
};
int main()
{
ifstream myfile;
myfile.open("List.
vectorvec;
istream_iterator studit(myfile);
istream_iterator end_of_stream;
copy(studit,end_of_stream,back_inserter(vec));
return 0;
}
Answer the question
In order to leave comments, you need to log in
This mess is hard to figure out, but the reason is obvious: lack of const
Student& operator=(Student& b) - should be Student& operator=( const Student& b)
Student(Student& a) - should be Student( const Student& a)
ostream& operator<<(ostream& s,Student& a) - should be ostream& operator<<(ostream& s, const Student& a)
etc.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question