2015-10-19 182 views
0

所以我查了一下,因为这对于大多数C++学生来说似乎是一个常见的作业问题,但我似乎无法找到能解答我的问题的问题。我觉得我已经正确填写了代码,但每次都会收到相同的错误。重载运算符Rational错误

这里是我的代码:

#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: '理性:: get_denominator':非标准语法;使用'&'创建指向成员的指针

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

3:错误C3867:'Rational :: get_numerator':非标准语法;使用“&”创建一个指向成员

我已经看过从已经完成的这个问题,并尝试他们的方法不同的在线网站的代码,但它似乎并没有工作。我已经将const和&添加到函数中的参数中,并且我仍然遇到相同的问题。我是否错误地调用函数或初始化一个错误?

回答

1

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

  1. 您返回的值不是参考值。
  2. 当你确定你不需要指定全名
  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; 
} 
+0

所以,当你的建议我使用set_function你的意思设置输入等于或者分子或分母。 '有理set_denom(int d){d = denom}'? – Motorscooter

+0

@Motorscooter我已经更新了答案。请注意,在你的评论中你正在做'd = denom';应该做相反的'denom = d'; – knightrider

+0

非常感谢您的澄清,这是现在有道理。只是为了确保..然后我会使用新的set_denom函数来设置我在每个函数中返回的有理值的分子和分母。 'add.set_num()&add.set_denom'?这样我没有返回一个私有变量的引用是否正确? – Motorscooter

0

您尝试调用该函数没有parethesis。它应该是get_denominator()

如果没有括号,你会得到指向该函数的指针,并尝试对它执行arythmetic - 因此出错。