好吧,我邪恶的编译器一直拒绝我的'=‘和'!=’重载操作符(我想)我的多项式类和有理类。非常感谢您的帮助。
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:29:21
是否尝试读取错误消息?
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 ==是布尔相等。从我所看到的你的代码中,看起来你把这两者弄混了。光是解决这个问题就能让你走上解决之路。
发布于 2010-12-02 13:29:19
错误消息实际上只是表示原样。例如:
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发布于 2010-12-02 13:31:07
虽然我坚持你应该开始接受答案,但不管怎样,我的答案是:
我认为你也应该重写这个方法:
Rational Rational::operator =(int a)
因为您在程序中的某个位置为Rational对象赋值了一个整数。
https://stackoverflow.com/questions/4331962
复制相似问题