作为一个赋值,我应该为分数创建一个Rational类,并重载各种运算符以操作分数。但是我在解决如何做到这一点时遇到了很多麻烦。这是我的代码和当前的错误消息。任何帮助将不胜感激!
编辑:感谢您的帮助。它仍然有两个错误消息。 一大堆(14+)个实例:未定义引用'Rational :: Rational()' 2:当我超载<<运算符它表示num和den未定义。如何告诉它从应该打印的对象中获取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;
}
答案 0 :(得分: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
或其他任何内容也是明智的。