gpt4 book ai didi

c++ - 理性类和 move 语义不起作用

转载 作者:行者123 更新时间:2023-11-28 04:45:37 24 4
gpt4 key购买 nike

我试图在问题中为已经给定的类(@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:我已经为类编写了“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&)'|

(据我所知,每当它被调用时, move 的对象/实例就会被销毁。)

谁能帮我实现这个类的 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/=() 应该是正确的,而不是对于应该返回一个新对象的 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);

而且,课外,

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

关于问题 2(“我已经为该类编写了“move ”构造函数,[...]但是,由于以下错误,我无法使用它”),您可以在 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.

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

添加即可解决问题

 Rational & operator= (Rational const &) = default;

重新激活隐式拷贝构造函数

关于c++ - 理性类和 move 语义不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49341961/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com