理性 class 和移动语义不起作用

Rational class and Move Semantics not working

我尝试在问题中已经给出的 class(@Aak 的答案)中添加更多功能:How to output fraction instead of decimal number? 用于在 numerator / denominator .

中打印分数

首先,如果不对代码进行任何更改,给定的代码将无法正常工作。然后我做了一些改变后让它工作。但是,我的实现给出了错误的输出。

例如:

input: A = 3;
       B = 3;
Output: 9/1
        9
instead of: 1

这里是完整的实现:

#include <iostream>
/********************** Rational class **********************************/
class Rational
{
private:
    int m_numerator, m_denominator;
private:
    inline void simplificate()
    {
        int commondivisor = 1;
        for(int i=2;i<= std::min(abs(m_numerator), abs(m_denominator));i++)
            if( m_numerator%i == 0 && m_denominator%i == 0 )
                commondivisor = i;
        m_numerator /= commondivisor;
        m_denominator /= commondivisor;
    }

public:
    Rational()                                  // Defualt
        :m_numerator(1), m_denominator(1)
        {}

    Rational(const int& num, const int& den=1)  // Parameterized
        :m_numerator(num), m_denominator(den)
        {}

    Rational(const Rational& other)             // Copy
        :m_numerator(other.m_numerator), m_denominator(other.m_denominator)
        {}

    /*Rational(Rational&& other)                  // Move
        :m_numerator(other.m_numerator), m_denominator(other.m_denominator)
        {}*/

    ~Rational(){}

    Rational& operator/ (const int& divisor)
    {
            m_denominator *= divisor;
            simplificate();
            return *this;
    }
    Rational& operator/ (const Rational &divisor)
    {
            m_numerator *= divisor.m_numerator;
            m_denominator *= divisor.m_denominator;
            simplificate();
            return *this;
    }
    const double getrealformat()const
    {
        return  static_cast<double>(m_numerator)/
                static_cast<double>(m_denominator);
    }

    friend double operator/ (Rational& obj, const int& divisor);
    friend void printRational(Rational& obj, const int& A, const int& B);
    friend void printRational(Rational& obj, const int&& A, const int&& B);
};
/************************** Friend functions ********************************/
double operator/ (Rational& obj, const int& divisor)
{
    obj.m_denominator *= divisor;
    obj.simplificate();
    return obj.getrealformat();
}
void printRational(Rational& obj, const int& A, const int& B)   // lvalue
{
    Rational r1(A), r2(B);
    obj = r1/r2;
    std::cout<<obj.m_numerator<<'/'<<obj.m_denominator<<std::endl;
    std::cout<<obj.getrealformat()<<std::endl;
}
void printRational(Rational& obj, const int&& A, const int&& B)  // rvalue
{
    Rational r1(A), r2(B);
    obj = r1/r2;
    std::cout<<obj.m_numerator<<'/'<<obj.m_denominator<<std::endl;
    std::cout<<obj.getrealformat()<<std::endl;
}
/*****************************************************************************/
int main()
{
    Rational obj;
    printRational(obj, 3,3);
    return 0;
}

问题- 1:逻辑看起来很好,但我不知道为什么我得到了错误的答案。有人能找到问题吗?

问题 - 2:我已经为 class 编写了 "Move" 构造函数,您可以在评论部分找到它。但是,由于以下错误,我无法使用它:

D:\Programming\C++\CPP Programs\Class - Fractions\Class - Fractions.cpp|70|error: use of deleted function 'Rational& Rational::operator=(const Rational&)'|

D:\Programming\C++\CPP Programs\Class - Fractions\Class - Fractions.cpp|77|error: use of deleted function 'Rational& Rational::operator=(const Rational&)'|

(据我所知,只要它被调用,被移动的 object/instance 就会被销毁。)

谁能帮我实现这个 class 的 Move 构造函数?

看看你的operator/()

Rational& operator/ (const Rational &divisor)
{
        m_numerator *= divisor.m_numerator;
        m_denominator *= divisor.m_denominator;
        simplificate();
        return *this;
}

此代码适用于 operator*(),不适用于 operator/()

可能

        m_numerator *= divisor.m_denominator;
        m_denominator *= divisor.m_numerator;

但更糟糕的是你 operator/() 修改对象。

您的代码(更正后的切换分子和分母)对于 operator/=() 应该是正确的,而不是对于应该 return 一个新对象的 operator/() 是正确的。

我建议如下

Rational& operator/= (const Rational &divisor)
{
        m_numerator *= divisor.m_denominator;
        m_denominator *= divisor.m_numerator;
        simplificate();
        return *this;
}

friend Rational operator/ (Rational A, Rational const & B);

并且,在 class、

之外
Rational operator/ (Rational A, Rational const & B)
 { return A/=B; }

关于问题 2(“我已经为 class 编写了“移动”构造函数,[...]但是,由于以下错误,我无法使用它”),您可以在 this page那个

A implicitly-declared copy assignment operator for class T is defined as deleted if any of the following is true:

  • T has a user-declared move constructor;
  • T has a user-declared move assignment operator.

因此,当您定义移动构造函数时,您删除了隐式复制运算符。

添加

即可解决问题
 Rational & operator= (Rational const &) = default;

重新激活隐式复制构造函数