Overloading Operator Rational Error

点点圈 提交于 2019-12-14 04:06:56

问题


So I have looked around because this seems to be a common homework problem for most C++ students, but I can't seem to find one that will answer my issue. I feel that I have filled out the code correctly but I get the same error each time.

Here is my code:

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

}

I know its a lot of code and I don't expect someone to go through it all debugging I just thought I would post it all just in case the problem spans farther then where I think the issue is.

I get the same errors for each operator:

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

I have looked at code from different online sites that have done this problem and tried their methods but it doesn't seem to work. I have added const and & to the parameters in the functions and I still get the same issues. Am I calling a function incorrectly or initializing one wrong?


回答1:


You have multiple problems in the code. Here is the corrected code.

  1. you were returning a value not a reference.
  2. when you are defining a function inside the class you dont need to specify the full name
  3. the () for function calls were missing

There are some comments on the code at the end.

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

}

Some comments on the code... Returning a reference, especially to a private member is really bad. I suggest you to create a set function.

so basically keep the get function as before

int get_denominator() const
{
    return denom;
}

and create a new function to set value

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



回答2:


You try to call the function without the parethesis. It should be get_denominator()

Without the parenthesis you get the pointer to the function instead and try to perform an arythmetic on it - hence the error.



来源:https://stackoverflow.com/questions/33222021/overloading-operator-rational-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!