重载运算符>>的使用是不明确的(有操作和类型的istream(也就是“basic_istream”)和“MyIncreEx”)

时间:2021-08-28 16:13:54

Here is the code and I don't seem to find what's wrong with it; I need to overload the << and >> operators, but I get the following error:

这是代码,我似乎没有发现它有什么问题;我需要重载< <和> >操作符,但我有以下错误:

Use of overloaded operator '>>' is ambiguous (with operand types 'istream' (aka 'basic_istream') and 'MyIncreEx')

重载运算符>>的使用是不明确的(有操作数类型的istream(又名“basic_istream”)和“MyIncreEx”)

I can't see what's really ambiguous about it:

我看不出这有什么不明确的地方:

#include<iostream>
using namespace std;


class MyIncreEx;
istream& operator>>(istream& is, MyIncreEx& s);
ostream& operator<<(ostream &os, MyIncreEx& s);
MyIncreEx operator++(MyIncreEx& d, int dummy);
MyIncreEx operator++(MyIncreEx& d);

class MyIncreEx
{
friend istream& operator>>(istream& is, MyIncreEx s);
friend ostream& operator<<(ostream& os, MyIncreEx s);
friend MyIncreEx operator++(MyIncreEx& d, int dummy);   //post-fix ex: x++;
friend MyIncreEx operator++(MyIncreEx& d);  //pre-fix ex: ++x;

public:
MyIncreEx(){num1=0; num2=0; num3=0;};

int num1;
int num2;
int num3;
};

//------------------------------------------------
istream& operator>>(istream& is, MyIncreEx& s)
{
is >> s.num1;
is >> s.num2;
is >> s.num3;
return is;
};

//------------------------------------------------
ostream& operator<<(ostream &os, MyIncreEx& s)
{
os << "(" << s.num1 <<"," <<s.num2 << "," << s.num3 <<")"<< endl;
return os;
};

//------------------------------------------------
MyIncreEx operator++(MyIncreEx& d)              //pre-fix   ex: ++x;
{
d.num1=d.num1+1;
d.num2=d.num2+1;
d.num3=d.num3+1;

return d;
};
//------------------------------------------------

MyIncreEx operator++(MyIncreEx& d, int dummy)   //post-fix ex: x++;
{
d.num1=d.num1+1;
d.num2=d.num2+1;
d.num3=d.num3+1;
return d;
};

//------------------------------------------------
int main()
{
MyIncreEx obj;

cout << "please enter three numbers: ";
cin  >> obj;
cout << "The original value are: ";
cout << obj;
cout << endl;
obj++;
cout << "The new values after obj++ are: ";
cout <<  obj;
cout << endl;
++obj;
cout << "The new values after ++obj are: ";
cout << obj;
cout << endl;

}
//------------------------------------------------

1 个解决方案

#1


3  

You declared two different versions of the output operators:

您声明了输出操作符的两个不同版本:

istream& operator>>(istream& is, MyIncreEx& s);
ostream& operator<<(ostream &os, MyIncreEx& s);

class MyIncreEx
{
    friend istream& operator>>(istream& is, MyIncreEx s);
    friend ostream& operator<<(ostream& os, MyIncreEx s);
    ...
};

The friend operators have a different and conflicting signature. You probably wanted to declare them as

朋友操作符有一个不同的、相互冲突的签名。您可能想要声明它们为。

    friend istream& operator>>(istream& is, MyIncreEx& s);
    friend ostream& operator<<(ostream& os, MyIncreEx const& s);

(assuming you also fix the output operator to work with MyIncreEx const& rather than MyIncreEx&).

(假设您还修复了输出操作符以使用MyIncreEx const&而不是MyIncreEx&)。

#1


3  

You declared two different versions of the output operators:

您声明了输出操作符的两个不同版本:

istream& operator>>(istream& is, MyIncreEx& s);
ostream& operator<<(ostream &os, MyIncreEx& s);

class MyIncreEx
{
    friend istream& operator>>(istream& is, MyIncreEx s);
    friend ostream& operator<<(ostream& os, MyIncreEx s);
    ...
};

The friend operators have a different and conflicting signature. You probably wanted to declare them as

朋友操作符有一个不同的、相互冲突的签名。您可能想要声明它们为。

    friend istream& operator>>(istream& is, MyIncreEx& s);
    friend ostream& operator<<(ostream& os, MyIncreEx const& s);

(assuming you also fix the output operator to work with MyIncreEx const& rather than MyIncreEx&).

(假设您还修复了输出操作符以使用MyIncreEx const&而不是MyIncreEx&)。