开发者

'=' AND '!=' operator overloading problem

开发者 https://www.devze.com 2023-01-28 16:48 出处:网络
Okay, my evil compiler keeps rejecting my \'=\' AND \'!=\' overloaded operators for (I think) both my Polynomial and Rational classes. Help is greatly appreciated.

Okay, my evil compiler keeps rejecting my '=' AND '!=' overloaded operators for (I think) both my Polynomial and Rational classes. Help is greatly appreciated.

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;
}

Here are my 3 error messages:

Polynomial.h(35) : error C2679: bin开发者_JS百科ary '=' : 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???


Try reading the error messages?

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

Both operator = implementations that I can see take a Rational& or a Polynomial& but none take an int. That being said there is clearly some information missing from your question.

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

Same problem.

All that aside, operator = is an assignment operator... operator == is Boolean equality. From what I can see of your code it looks like you have the two mixed up. Fixing that alone would set you well on your way to a solution.


The error messages actually just mean as they are. For instance:

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)'

Because you define your overloading as

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

So it only accepts things like:

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

but not:

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


While I insist that you should indeed start accepting answers, here's my answer anyway:

I think you should override this method too:

Rational Rational::operator =(int a)

Because you're assigning your Rational object with an integer somewhere in your program.

0

精彩评论

暂无评论...
验证码 换一张
取 消