'='AND'!='运算符重载问题

Mik*_*ike 0 c++

好吧,我的邪恶编译器一直拒绝我的'='AND'!='重载操作符(我认为)我的Polynomial和Rational类.非常感谢帮助.

using namespace std;

class Polynomial
{

public:

//constructor
Polynomial() 
    {
        for ( int i = 0; i < 100; i++ )
    {
        coefficient[i] = 0;
    }
    }

~Polynomial(){}

void polynomialSet ( Rational a , int b ) //polynomialSetter function
    {  
    coefficient[b] = a;
    exponent = b;
}

    . . . . 

    const Polynomial &Polynomial::operator=(const Polynomial &a)
{
    if (&a == this)
        return *this;
}

bool Polynomial::operator!=(Polynomial &a)
{       
    return !(*this == a);
    }

***************************************************************************
using namespace std;

class Rational {

public:
//constructors

Rational (int a, int b)
{
//Rational( const int &a, const int &b){
    if (a != 0)
    {
        if (b != 0)
        {
            this->numerator = a;
            this->denominator = b;
        }
    }
}

Rational(){}

~Rational() {}

    . . . .

    bool Rational::operator!=(Rational a)
{
    return (a.numerator != numerator) && (a.denominator != denominator);
}

Rational Rational::operator =(const Rational &a)
{
    this->numerator = a.numerator;
    this->denominator = a.denominator;
    return *this;
}
Run Code Online (Sandbox Code Playgroud)

这是我的3条错误消息:

Polynomial.h(35) : error C2679: binary '=' : no operator found which takes a
right-hand operand of type 'int' (or there is no acceptable conversion)
Rational.h(99): could be 'Rational Rational::operator =(const Rational &)'
while trying to match the argument list '(Rational, int)'

Polynomial.h(53) : error C2679: binary '!=' : no operator found which takes a  
right-hand operand of type 'int' (or there is no acceptable conversion)
Rational.h(94): could be 'bool Rational::operator !=(Rational)'
while trying to match the argument list '(Rational, int)'

Polynomial.h(63) : error C2679: binary '!=' : no operator found which takes a
right-hand operand of type 'int' (or there is no acceptable conversion)
Rational.h(94): could be 'bool Rational::operator !=(Rational)'
while trying to match the argument list '(Rational, int)'

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Run Code Online (Sandbox Code Playgroud)

救命???

Mat*_*lin 6

尝试阅读错误消息?

binary '=' .... right-hand operand of type 'int' .... while trying to match the argument list '(Rational, int)'
Run Code Online (Sandbox Code Playgroud)

我可以看到的operator = implementation都采用了a Rational&或者a,Polynomial&但是没有采用int.话虽如此,你的问题显然缺少一些信息.

binary '!=' .... right-hand operand of type 'int' .... hile trying to match the argument list '(Rational, int)'
Run Code Online (Sandbox Code Playgroud)

同样的问题.

除此之外,operator =是赋值运算符... operator ==是布尔相等.从我所看到的你的代码看起来你有两个混淆.单独解决这个问题可以帮助您找到解决方案的方法.