首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >'=‘和'!=’运算符重载问题

'=‘和'!=’运算符重载问题
EN

Stack Overflow用户
提问于 2010-12-02 13:23:30
回答 3查看 267关注 0票数 0

好吧,我邪恶的编译器一直拒绝我的'=‘和'!=’重载操作符(我想)我的多项式类和有理类。非常感谢您的帮助。

代码语言:javascript
运行
复制
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条错误消息:

代码语言:javascript
运行
复制
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 ==========

帮帮忙?

EN

Stack Overflow用户

发布于 2010-12-02 13:31:07

虽然我坚持你应该开始接受答案,但不管怎样,我的答案是:

我认为你也应该重写这个方法:

Rational Rational::operator =(int a)

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

票数 0
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4331962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档