重载运算符有理错误

编程入门 行业动态 更新时间:2024-10-22 23:10:30
本文介绍了重载运算符有理错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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.

这是我的代码:

#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:错误C3867:"Rational :: get_denominator":非标准语法;使用&"创建指向成员的指针

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

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

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

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

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

我查看了来自完成此问题的不同在线站点的代码,并尝试了他们的方法,但似乎不起作用.我添加了const和&到函数中的参数,我仍然遇到相同的问题.我是错误地调用一个函数还是初始化一个错误?

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?

推荐答案

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

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

  • 您返回的值不是引用.
  • 在类中定义函数时,无需指定全名
  • 缺少函数调用的()
  • 在代码末尾有一些注释.

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

    对代码的一些注释...返回引用,尤其是返回给私有成员真的很糟糕.我建议您创建一个set函数.

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

    因此基本上像以前一样保留get函数

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

    更多推荐

    重载运算符有理错误

    本文发布于:2023-06-03 12:17:37,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/473763.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:有理   运算符   错误

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!