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

时间:2010-12-02 05:23:30

标签: 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;
}

以下是我的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 ==========

HELP ???

3 个答案:

答案 0 :(得分:6)

尝试阅读错误消息?

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

我可以看到的operator =实现都采用Rational&Polynomial&但没有采用int。话虽如此,你的问题显然缺少一些信息。

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

同样的问题。

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

答案 1 :(得分: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)'

因为您将重载定义为

Rational Rational::operator =(const Rational &a)

因此它只接受以下内容:

Rational r1 = new Rational(3, 5);
Rational r2 = r1; // calls r2.operator=(r1);

但不是:

r2 = 3; // wrong, as the right hand operand is int, not Rational

答案 2 :(得分:0)

虽然我坚持你应该开始接受答案,但无论如何,这是我的答案:

我认为你也应该覆盖这个方法:

Rational Rational :: operator =(int a)

因为您在程序中的某个位置为Rational对象分配了一个整数。