好吧,我邪恶的编译器一直拒绝我的'=‘和'!=’重载操作符(我想)我的多项式类和有理类。非常感谢您的帮助。
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 ==========帮帮忙?
发布于 2010-12-02 13:31:07
虽然我坚持你应该开始接受答案,但不管怎样,我的答案是:
我认为你也应该重写这个方法:
Rational Rational::operator =(int a)
因为您在程序中的某个位置为Rational对象赋值了一个整数。
https://stackoverflow.com/questions/4331962
复制相似问题