重载运算符理性错误

Overloading Operator Rational Error

所以我环顾四周,因为这似乎是大多数 C++ 学生的常见家庭作业问题,但我似乎找不到可以回答我的问题的问题。我觉得我已经正确填写了代码,但每次都得到相同的错误。

这是我的代码:

#include <iostream>

using namespace std;

class Rational
{
public:
Rational() { 
    num = 0;
    denom = 1;
};
Rational(int n, int d) { 
    
    num = n;
    denom = d;
    normalize();
}
Rational(int n) { 
    num = n;
    denom = 1;
}
int get_numerator() const { 
    
    return num;

}
int get_denominator() const { 
    return denom;
}
void normalize() { 
    if ((num > 0 && denom < 0)||(num < 0 && denom < 0)) {
        num = -1 * num;
        denom = -1 * denom;
    }
    int gcdcheck = GCD(num,denom);
    num = num / gcdcheck;
    denom = denom / gcdcheck;

}
int Rational::GCD(int n, int d) {
    int temp;
    n = abs(n);
    d = abs(d);
    if (n > d) {
    // Do nothing everything is where it should be
    }
    else {
        temp = n;
        n = d;
        d = temp;
    }
    int factor = n % d;
    while (factor != 0) {
        factor = n % d;
        d = n;
        n = factor;

    }
    return d;//Return the value to normalize to simplify the fractions to      simplist form
}
Rational operator+(Rational b) const { 
    Rational add;
    //Addition of fractions (a*d/b*d + c*b/d*b)
    //Numerator = (a*d + c*b)
    add.get_numerator = b.get_numerator * denom + b.get_denominator * num;
    //Denomenator = (b*d)
    add.get_denominator = b.get_denominator * denom;
    add.normalize();
    return add;

}
Rational operator-(Rational b) const {
    Rational sub;
    //Same as Addition just a minus sign
    //Numerator = (a*d + c*b)
    sub.get_numerator = b.get_numerator * denom + b.get_denominator * num;
    //Denomenator = (b*d)
    sub.get_denominator = b.get_denominator * denom;
    sub.normalize();
    return sub;
}

Rational operator*(Rational b) const { 
//Multiply the numerators and denomenators
    Rational multi;


    multi.get_numerator = b.get_numerator * num;
    multi.get_denominator = b.get_denominator * denom;
    multi.normalize();

    return multi;
}
Rational operator/(Rational b) const { 
    //Division of fractions is done by the recipricol of one of the fractions
    Rational divi;
    divi.get_numerator = b.get_numerator * denom;
    divi.get_denominator = b.get_denominator * num;
    divi.normalize();
    return divi;
}

//To avoid issues with rounding the compare functions will multiply instead to give clean whole numbers
//This will be done by multiplying the denomenators by the opposite numerator
bool operator==(Rational b) const { 
    return ((b.get_numerator * denom == b.get_denominator * num));

}
bool operator<(Rational b) const { 
    return ((b.get_numerator * denom > b.get_denominator * num));
}
double toDecimal() const { 
    double result;
    result = static_cast<double> (num)/ static_cast<double> (denom);
    
    return result;
    
}
private:
int num = 0; // default value is 0
int denom = 1; // default value is 1
};
ostream& operator<<(std::ostream& output, Rational& a) {
if (a.get_denominator == 0) {
    output << "Divide by Zero";


}
output << a.get_numerator << '/' << a.get_denominator;
return output;

}

我知道它有很多代码,我不希望有人调试它我只是想我会 post 以防万一问题超出我认为的问题是。

每个运算符都出现相同的错误:

1: error C3867: 'Rational::get_denominator': non-standard syntax; use '&' to create a pointer to member

2: '*': error C3867: 'Rational::get_denominator': non-standard syntax; use '&' to create a pointer to member

3: error C3867: 'Rational::get_numerator': non-standard syntax; use '&' to create a pointer to member

我查看了解决此问题的不同在线站点的代码并尝试了他们的方法,但它似乎不起作用。我在函数的参数中添加了 const 和 &,但我仍然遇到同样的问题。我是调用函数错误还是初始化错误?

您尝试调用不带括号的函数。应该是get_denominator()

如果没有括号,您将获得指向函数的指针,并尝试对其执行算术运算 - 因此会出现错误。

您的代码有多个问题。这是更正后的代码。

  1. 您返回的是一个值而不是一个引用。
  2. 当您在 class 中定义函数时,您不需要指定全名
  3. 缺少函数调用的 ()

代码最后有一些注释。

#include <iostream>
#include <cmath>
using namespace std;

class Rational
{
public:
    Rational()
    {
        num = 0;
        denom = 1;
    };
    Rational(int n, int d)
    {`

        num = n;
        denom = d;
        normalize();
    }
    Rational(int n)
    {
        num = n;
        denom = 1;
    }
    int& get_numerator() 
    {

        return num;

    }
    int& get_denominator() 
    {
        return denom;
    }
    void normalize()
    {
        if ((num > 0 && denom < 0) || (num < 0 && denom < 0))
        {
            num = -1 * num;
            denom = -1 * denom;
        }
        int gcdcheck = GCD(num, denom);
        num = num / gcdcheck;
        denom = denom / gcdcheck;

    }
    int GCD(int n, int d)
    {
        int temp;
        n = abs(n);
        d = abs(d);
        if (n > d)
        {
            // Do nothing everything is where it should be
        }
        else
        {
            temp = n;
            n = d;
            d = temp;
        }
        int factor = n % d;
        while (factor != 0)
        {
            factor = n % d;
            d = n;
            n = factor;

        }
        return d;//Return the value to normalize to simplify the fractions to      simplist form
    }
    Rational operator+(Rational b) const
    {
        Rational add;
        //Addition of fractions (a*d/b*d + c*b/d*b)
        //Numerator = (a*d + c*b)
        add.get_numerator()= b.get_numerator() * denom + b.get_denominator() * num;
        //Denomenator = (b*d)
        add.get_denominator() = b.get_denominator() * denom;
        add.normalize();
        return add;

    }
    Rational operator-(Rational b) const
    {
        Rational sub;
        //Same as Addition just a minus sign
        //Numerator = (a*d + c*b)
        sub.get_numerator() = b.get_numerator() * denom + b.get_denominator() * num;
        //Denomenator = (b*d)
        sub.get_denominator() = b.get_denominator() * denom;
        sub.normalize();
        return sub;
    }

    Rational operator*(Rational b) const
    {
//Multiply the numerators and denomenators
        Rational multi;


        multi.get_numerator() = b.get_numerator() * num;
        multi.get_denominator() = b.get_denominator() * denom;
        multi.normalize();

        return multi;
    }
    Rational operator/(Rational b) const
    {
        //Division of fractions is done by the recipricol of one of the fractions
        Rational divi;
        divi.get_numerator() = b.get_numerator() * denom;
        divi.get_denominator() = b.get_denominator() * num;
        divi.normalize();
        return divi;
    }

//To avoid issues with rounding the compare functions will multiply instead to give clean whole numbers
//This will be done by multiplying the denomenators by the opposite numerator
    bool operator==(Rational b) const
    {
        return ((b.get_numerator() * denom == b.get_denominator() * num));

    }
    bool operator<(Rational b) const
    {
        return ((b.get_numerator() * denom > b.get_denominator() * num));
    }
    double toDecimal() const
    {
        double result;
        result = static_cast<double> (num) / static_cast<double> (denom);

        return result;

    }
private:
    int num = 0; // default value is 0
    int denom = 1; // default value is 1
};
ostream& operator<<(std::ostream& output, Rational& a)
{
    if (a.get_denominator() == 0)
    {
        output << "Divide by Zero";


    }
    output << a.get_numerator() << '/' << a.get_denominator();
    return output;

}

对代码的一些评论...返回引用,尤其是对私有成员的引用真的很糟糕。我建议你创建一个集合函数。

所以基本保持get函数不变

int get_denominator() const
{
    return denom;
}

并创建一个新函数来设置值

int set_denominator(int in) 
{
    denom = in;
}