【问题标题】:Fraction class troubleshooting [closed]分数类故障排除[关闭]
【发布时间】:2014-02-14 02:37:44
【问题描述】:

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

编辑:感谢您的帮助。它仍然有两个错误消息。 大量(14+)个实例:未定义的对“Rational::Rational()”的引用 2:当我重载

头文件

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

}

【问题讨论】:

  • 你的问题是......
  • 在写代码的时候,一遇到错误就停止。在修复该错误之前不要继续。
  • 你做错了很多事情。其中一些列在我的回答中。
  • @Beta 在这一点上我很迷茫,我不知道我在做什么。而且我不知道哪些是错误,哪些不是,因为我不知道它应该是什么样子。在你说之前,我已经尝试过在网上看等等。但这只会有很大帮助。
  • 您应该显示所有错误。在修复错误之前它永远不会工作。

标签: c++ operator-overloading


【解决方案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 或其他任何内容也是明智的。

【讨论】:

  • 我改变了那些。非常感谢。
猜你喜欢
  • 2017-08-01
  • 2023-03-08
  • 1970-01-01
  • 2014-04-25
  • 1970-01-01
  • 2013-07-15
  • 2010-11-30
  • 2021-09-11
  • 2012-02-18
相关资源
最近更新 更多