【问题标题】:Overloading Operator Rational Error重载运算符理性错误
【发布时间】:2015-10-19 19:07:58
【问题描述】:

所以我环顾四周,因为这似乎是大多数 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;

}

我知道它有很多代码,我不希望有人通过所有调试我只是想我会把它全部发布,以防问题超出我认为的问题所在。

我得到每个操作员相同的错误:

1:错误 C3867:'Rational::get_denominator':非标准语法;使用 '&' 创建指向成员的指针

2:“*”:错误 C3867:“Rational::get_denominator”:非标准语法;使用 '&' 创建指向成员的指针

3: 错误 C3867: 'Rational::get_numerator': 非标准语法;使用 '&' 创建指向成员的指针

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

【问题讨论】:

    标签: operator-overloading


    【解决方案1】:

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

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

    代码最后有一些cmets。

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

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

    所以基本上保持get函数和以前一样

    int get_denominator() const
    {
        return denom;
    }
    

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

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

    【讨论】:

    • 因此,当您建议我使用 set_function 时,您的意思是设置输入等于分子或分母。 Rational set_denom(int d) {d = denom}?
    • @Motorscooter 我已经更新了答案。请注意,在您的评论中,您正在做d = denom;应该做相反的denom = d;
    • 非常感谢您的澄清,现在这很有意义。只是为了确保..然后我会使用新的 set_denom 函数来设置我在每个函数中返回的有理值的分子和分母? add.set_num() &amp; add.set_denom?这样我就不会返回对私有变量的引用正确吗?
    • @Motorscooter 是的,你是对的。你应该传递你想要设置的值。 IE。 add.set_num(10); //replace 10 with the value you wan to set
    【解决方案2】:

    您尝试在没有括号的情况下调用该函数。应该是get_denominator()

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

    【讨论】:

      猜你喜欢
      • 2010-12-23
      • 2013-11-14
      • 2016-10-18
      • 1970-01-01
      • 1970-01-01
      • 2017-08-10
      • 2015-07-04
      • 1970-01-01
      相关资源
      最近更新 更多