输入操作符重载问题 operator>> is ambiguous
#include<iostream>#include<stdlib.h>
using namespace std;
class SmallInt
{
public:
SmallInt(int i=0):value(rangecheck(i))
{}
int operator=(int i)
{
return (value=rangecheck(i));
}
operator int()
{
return value;
}
friend istream& operator>>(istream &is,SmallInt &s);
friend ostream& operator<<(ostream &os,const SmallInt &s)
{
return os<<s.value;
}
private:
int rangecheck(int);
int value;
};
istream& operator>>(istream &is,SmallInt &s)
{
int ix;
is>>ix;
s=ix;
return is;
}
int SmallInt::rangecheck(int i)
{
if(i&~0377)
{
cerr<<"\n***SmallInt range error: "
<<i<<" ***"<<endl;
exit(-1);
}
return i;
}
int main()
{
SmallInt s1,s2;
cout<<"enter a SmallInt , please: ";
while(1)
{
cin>>s1;
cout<<"the value read is "
<<s1<<"\nit is ";
cout<<((s1>127) ? "greater than" :((s1<127) ? "less than" : "equal to"))<<"127\n";
cout<<"\nenter a SmallInt ,please \
(ctrl-d to exit): ";
}
cout<<"bye now\n";
system("pause");
return 0;
}
这是c++ primer3 上的一个例子,编译以后出现'operator >>' is ambiguous 的错误提示
但将头文件换成#include<iostream.h>就可以编译通过而且可以正常运行,这是怎么回事啊