2014-02-14 217 views
-2

作为一项任务,我应该为分数创建一个Rational类,并重载各种操作符以操纵分数。但我在清理如何做这件事方面遇到了很多麻烦。这是我的代码和当前的错误消息。任何帮助将不胜感激!分数类故障排除

编辑:感谢您的帮助。它仍然有两个错误消息。 一吨(14+)的实例:未定义的引用'Rational :: Rational()' 2:当我超载< <运算符它说num和den没有定义。我如何告诉它从它应该打印出来的对象中获取数字和书房? 谢谢!

头文件

#ifndef Rational_H 
#define Rational_H 

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

class Rational 
{ 

    friend Rational operator+(const Rational& x1, const Rational& x2); 
    friend Rational operator-(const Rational& x1, const Rational& x2); 
    friend Rational operator*(const Rational& x1, const Rational& x2); 

public: 
    Rational(); //constructor 
    void setFraction(int, int); //set fractional 
    int getNum() const; 
    int getDen() const; 

    void RSimplify(); 
    void printFraction(); 
    friend ostream &operator<<(ostream &, const Rational &); 
    friend istream &operator>>(istream &, Rational &); 

private: 
    int num; 
    int den; 
}; 

#endif 

.cpp文件

#include <iomanip> 
#include "Rational.h" 
using namespace std; 


int Rational::getNum() const 
{ 
    return num; 
} 

int Rational::getDen() const 
{ 
    return den; 
} 

ostream &operator<<(ostream &output, const ATJRational &ATJRational) 
{ 
    return output << num << "/" << den; 
Error: num and den are not declared in this scope 
} 
istream &operator>>(istream &input, ATJRational &ATJRational) 
{ 
    char slash; 
    return input >> ATJRational.num >> slash >> ATJRational.den; 
    ATJRational.RSimplify(); 
} 

void ATJRational::RSimplify() 
{ 
    for(int i=2; i<14; i++) 
    { 
     while(den % i == 0) 
     { 
      if(num % i == 0) 
      { 
       den = den/i; 
       num = num/i; 
      } 
     } 
    } 
} 


Rational operator+(const Rational& x1, const Rational& x2) 
    { 
    Rational x3;  
    x3.num = (x1.num * x2.den) + (x2.num * x1.den); 
    x3.den = (x1.den * x2.den); 
    x3.RSimplify(); 
    return x3; 
    } 

Rational operator-(const Rational& x1, const Rational& x2) 
    { 
    Rational x3;  // result 
    x3.num = (x1.num * x2.den) - (x2.num * x1.den); 
    x3.den = (x1.den * x2.den); 
    x3.RSimplify(); 
    return x3; 
    } 

Rational operator*(const Rational& x1, const Rational& x2) 
    { 
    Rational x3;  // result 
    x3.num = (x1.num * x2.num); 
    x3.den = (x1.den * x2.den); 
    x3.RSimplify(); 
    return x3; 
    } 

主文件

#include <iostream> 
#include "Rational.h" 
using namespace std; 

int main() 
{ 
    Rational x1; //create object fraction 
    Rational x2; 
    Rational x3; 

    cout<<"Enter a fraction in the form 1/4:"<<endl; 
    cin>>x1; 

    cout<<"Enter a 2nd fraction in the form 1/4:"<<endl; 
    cin>>x2; 

    cout<<"The two fractions in their lowest terms are: "<<x1<<" and "<<x2<<endl; 

    cout<<"Adding the 1st and 2nd fractions together..."<<endl; 
    x3 = x1 + x2; 
    cout<<"The sum of the two fractions is:"<<x3<<endl; 

    cout<<"Subtracting the 1st fraction from the 2nd fraction..."<<endl; 
    x3 = x2 - x1; 
    cout<<"The result is:"<<x3<<endl; 

    cout<<"Multiplying the 1st fraction by the 2nd fraction..."<<endl; 
    x3 = x1 * x2; 
    cout<<"The product is:"<<x3<<endl; 

} 
+4

而你的问题是...... – yizzlez

+2

当你写代码时,只要你遇到一个错误,* stop *。不要继续,直到你修复了这个错误。 – Beta

+0

你做错了很多事情。其中一些列在我的答案中。 – juanchopanza

回答

1

忽略任何逻辑错误,我可以看到一些问题。可能还有更多。

首先,这

RSimplify.x3; 

应该

x3.RSimplify(); 

其次,你的算术运算符的定义不匹配的朋友的声明。这是因为丢失了一个RHS const

Rational operator+(const Rational& x1, Rational& x2) { .... } 

必须

Rational operator+(const Rational& x1, const Rational& x2) { .... } 

三,输出流操作者应作用于你通过它的流,没有明确cout。所以,

ostream &operator<<(ostream &output, const Rational &Rational) 
{ 
    return output << num << "/" << den; 
} 

注意它也是明智的,它留给呼叫者添加endl或其他任何东西。

+0

我改变了这些。非常感谢。 – Neko