Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include<iostream>
using namespace std;

template<class In,class Out>
Out copy1(In start,In end,Out dest)
{
while(start!=end){
*dest++=*start++;
}
return dest;
}

template<class T>
class ostream_iterator{
public:
ostream_iterator(ostream& os,const char*s):strm(&os),str(s){}
ostream_iterator& operator++(){return *this;}
ostream_iterator& operator++(int){return *this;}
ostream_iterator& operator*(){return *this;}
ostream_iterator& operator=(const T &t){
*strm<<t<<str;
return *this;
}
private:
ostream* strm;
const char *str;
};

template<class T>
class istream_iterator{
friend int operator==<>(istream_iterator&,istream_iterator&);
friend int operator!=<>(istream_iterator&,istream_iterator&);
public:
istream_iterator(istream& is):strm(&is),full(0),eof(0){}
istream_iterator():strm(0),full(0),eof(1){}
istream_iterator& operator++(){
full=0;
return *this;
}
istream_iterator operator++(int){
istream_iterator r=*this;
full=0;
return r;
}
T operator*(){
fill();
assert(full);
return buffer;
}

private:
T buffer;
istream *strm;
int full;
int eof;
void fill(){
if(!full&&!eof){
if(*strm>>buffer)full=1;
else eof=1;
}
}
};

template<class T>
int operator==(istream_iterator<T>& p,istream_iterator<T>& q)
{
if(p.eof&&q.eof)return 1;
if(!p.eof&&!q.eof)return &p==&q;
p.fill();
q.fill();
return p.eof==q.eof;
}
template<class T>
int operator!=(istream_iterator<T>& p,istream_iterator<T>& q)
{
return !(p==q);
}

int main()
{
ostream_iterator<int> output(cout,"\n");
istream_iterator<int> input(cin);
istream_iterator<int> e;
copy1(input,e,output);
system("pause");
return 0;
}


Istream_iterator class == and!= overloading is wrong.Please help me,thank you!!
Posted
Updated 21-Oct-13 20:30pm
v2
Comments
Freak30 22-Oct-13 3:55am    
You are never calling the == operator of the istream_iterator. You only compare the adresses of p and q.
Dark Darkling 22-Oct-13 5:05am    
Why? It's a reference not a pointer, so IMHO this call OK.
But I guess it makes sense to return bool from comparison operators like:
template<class t="">
bool operator==(istream_iterator<t>& p,istream_iterator<t>& q)
{
// Body
}
Freak30 22-Oct-13 5:12am    
If you write
return &p==&q;
you compare the adresses of the objects.
What you need to do to compare the objects (and make use of the == operator) is
return p==q;
Dark Darkling 22-Oct-13 5:18am    
Of course, you are exactly right. In additional it's needed to write a correct body, specifically change 'if(!p.eof&&!q.eof)return &p==&q;' to 'if(!p.eof&&!q.eof)return p==q;'
Philippe Mori 22-Oct-13 18:35pm    
Why are you using int instead of bool. bool is supported by all C++ compiler since long time.

1 solution

The error is that you try to get the addresses of two reference variables and compare them.

&p is a memory location and &q is another memory location. This, &p==&q, always returns false except in the following case of using your == operator.

if(a == a). (a is an object of type istream_iterator).

If you want to compare the 2 integer variables (p and q), use the following instead of your line of code.

C++
if(!p.eof && !q.eof)return p==q;
 
Share this answer
 
v2
Comments
Philippe Mori 22-Oct-13 18:25pm    
if a is 0 and b is not 0 (or vice versa) then first side (!a && !b) is false and second side !(a && b) is true thus not equal.

Thus it make most of your answer wrong as "useless" code will be executed if only one stream is at EOF.
Captain Price 22-Oct-13 21:17pm    
Very true. Thank you for pointing that out. If you've not, this answer is completely wrong. I have updated my post. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900