'='AND'!='运算符重载问题 [英] '=' AND '!=' operator overloading problem

查看:181
本文介绍了'='AND'!='运算符重载问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我的邪恶编译器不断拒绝我的'='AND'!='重载运算符(我认为)我的多项式和Rational类。非常感谢您的帮助。

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

这是我的3条错误消息:

Here are my 3 error messages:

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 ???

推荐答案

尝试阅读错误讯息? / p>

Try reading the error messages?

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

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

相同的问题。

除此之外, operator = 是一个赋值运算符... operator == 是布尔等式。从我可以看到你的代码看起来像你有两个混合。单独修复这个问题会让你很快找到解决方案。

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.

这篇关于'='AND'!='运算符重载问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆