c++运算符重载=在visual studio中运行不佳但在eclipse CDT中运行

标签 c++ visual-studio

我正在重写前一段时间类的一些代码,原始代码在 eclipse CDT 中,现在我正在尝试使用 visual studio,那是我重写代码的地方,但在 visual studio 中它不能很好地计算赋值运算符 (=) 并放入垃圾值,而在 eclipse 中它运行良好并给出 Fraction 最终简化答案。如果您发现我的代码有问题,请告诉我。我正在尝试程序的 sum 选项,代码在 f3 = f1 + f2; 行中断。 运行示例:

Code running in Eclipse CDT
Welcome to the fractions operations tutor
------------------------------------------------------
Option 1 - Sum of Fractions
Option 2 - Subtraction of Fractions
Option 3 - Multiplication of Fractions
Option 4 - Division de Fractions
Option 5 - Comparare Fractions
Option 6 - Exit
------------------------------------------------------------
1
Enter the numerator:2
Enter the denominator:3
Enter the numerator:3
Enter the denominator:3
2     3     5
-- + --   = --
3     3     3
Enter -1 if you want to exit this level,
If you wish to continue press a diferent number.
Run in vscode
Welcome to the fractions
-------------------------
Option 1 - Sum of Fractio
Option 2 - Subtraction of
Option 3 - Multiplication
Option 4 - Division de Fr
Option 5 - Comparare Frac
Option 6 - Exit
-------------------------
1
Enter the numerator:2
Enter the denominator:3
Enter the numerator:3
Enter the denominator:3
2     3     -858993460
-- + --   = --
3     3     -858993460
#include <iostream>
using namespace std;
class Fraction
{
private:
    int numerator, denominator;
    int GreatestCommonDenominator(Fraction& tempFraction);
public:
    Fraction(int = 1, int = 1);
    Fraction(const Fraction&);
    ~Fraction();
    void setFraction(int, int);
    void setNumerator(int);
    void setDenominator(int);
    int getNumerator() const;
    int getDenominator() const;
    void changeSign();
    void displayMenu(int& option);
    void exitDisplayMenu(int& out);
    void displayFraction() const;
    void sumTwoFractions(Fraction);
    void divideTwoFractions(Fraction);

    Fraction &operator=(const Fraction& tempFraction);
    Fraction &operator+(const Fraction& tempFraction);
    Fraction &operator-(const Fraction& tempFraction);
    Fraction& operator*(const Fraction& tempFraction);
    Fraction& operator/(const Fraction& tempFraction);

    bool operator <(const Fraction&);
    bool operator >(const Fraction&);
    bool operator ==(const Fraction&);
    bool operator !=(const Fraction&);

    friend istream& operator>>(istream&, Fraction&);
    friend ostream& operator<<(ostream&, const Fraction&);


};

分数.cpp

#include "Fraction.h"

Fraction::Fraction(int tempNumerator, int tempDenominator) :numerator(tempNumerator), denominator(tempDenominator)
{
}

Fraction::Fraction(const Fraction& tempFraction)
{
    setNumerator(tempFraction.getNumerator());
    setDenominator(tempFraction.getDenominator());
}

Fraction::~Fraction() 
{
}

void Fraction::setFraction(int tempNumerator, int tempDenominator)
{
    setNumerator(tempNumerator);
    setDenominator(tempDenominator);
}

void Fraction::setNumerator(int tempNumerator)
{
    this->numerator = tempNumerator;
}

void Fraction::setDenominator(int tempDenominator)
{
    if (tempDenominator != 0)
    {
        this->denominator = tempDenominator;
    }
    else
    {
        while (tempDenominator == 0)
        {
            cout << "The denominator can't be zero. Re-enter:";
            cin >> tempDenominator;
            this->denominator = tempDenominator;
        }
    }
}

int Fraction::getNumerator() const
{
    return this->numerator;
}

int Fraction::getDenominator() const
{
    return this->denominator;
}

void Fraction::changeSign()
{
    setNumerator(-1 * getNumerator());
}

void Fraction::displayMenu(int& option)
{
    cout << "Welcome to the fractions operations tutor" << endl;
    cout << "------------------------------------------------------" << endl;
    cout << "Option 1 - Sum of Fractions" << endl;
    cout << "Option 2 - Subtraction of Fractions" << endl;
    cout << "Option 3 - Multiplication of Fractions" << endl;
    cout << "Option 4 - Division de Fractions" << endl;
    cout << "Option 5 - Comparare Fractions" << endl;
    cout << "Option 6 - Exit" << endl;
    cout << "------------------------------------------------------------" << endl;
    cin >> option;
}

void Fraction::exitDisplayMenu(int& out)
{
    cout << "Enter -1 if you want to exit this level,\n"
        << "If you wish to continue press a diferent number.";
    cin >> out;
}

void Fraction::displayFraction() const
{
    cout << getNumerator() << endl;
    cout << "--" << endl;
    cout << getDenominator() << endl;

}

void Fraction::sumTwoFractions(Fraction tempFraction)
{
    setNumerator(getNumerator() * tempFraction.getDenominator() + getDenominator() * tempFraction.getNumerator());
    setDenominator(getDenominator() * tempFraction.getDenominator());
}

void Fraction::divideTwoFractions(Fraction tempFraction)
{
    setNumerator(getNumerator() * tempFraction.getDenominator());
    setDenominator(getDenominator() * tempFraction.getNumerator());
}

int Fraction::GreatestCommonDenominator(Fraction& tempFraction)
{
    int gcd;
    int y;
    if (tempFraction.getNumerator() > tempFraction.getDenominator())
    {
        gcd = tempFraction.getNumerator();
        y = tempFraction.getDenominator();
    }
    else
    {
        gcd = tempFraction.getDenominator();
        y = tempFraction.getNumerator();
    }
    while (y != 0)
    {
        int r = gcd % y;
        gcd = y;
        y = r;
    }
    return gcd;
}

Fraction& Fraction::operator+(const Fraction& tempFraction)
{
    Fraction sum;
    sum.setNumerator((this->numerator * tempFraction.getDenominator()) + (this->denominator * tempFraction.getNumerator()));
    sum.setDenominator(this->denominator * tempFraction.getDenominator());
    int gcd = GreatestCommonDenominator(sum);
    sum.setNumerator(sum.numerator / gcd);
    sum.setDenominator(sum.denominator / gcd);
    return sum;
}

Fraction& Fraction::operator-(const Fraction& tempFraction)
{
    Fraction subtract;
    subtract.setNumerator((this->numerator * tempFraction.getDenominator()) - (this->denominator * tempFraction.getNumerator()));
    subtract.setDenominator(this->denominator * tempFraction.getDenominator());
    int gcd = GreatestCommonDenominator(subtract);
    subtract.setNumerator(subtract.numerator / gcd);
    subtract.setDenominator(subtract.denominator / gcd);
    return subtract;
}

Fraction& Fraction::operator *(const Fraction& tempFraccion) 
{
    Fraction multiply;
    multiply.setNumerator((this->numerator) * (tempFraccion.getNumerator()));
    multiply.setDenominator((this->denominator) * (tempFraccion.getDenominator()));
    int gcd = GreatestCommonDenominator(multiply);
    multiply.setNumerator(multiply.numerator / gcd);
    multiply.setDenominator(multiply.denominator / gcd);
    return multiply;
}

Fraction& Fraction::operator /(const Fraction& tempFraccion) 
{
    Fraction divide;
    divide.setNumerator((this->numerator) * (tempFraccion.getDenominator()));
    divide.setDenominator((this->denominator) * (tempFraccion.getNumerator()));
    int gcd = GreatestCommonDenominator(divide);
    divide.setNumerator(divide.numerator / gcd);
    divide.setDenominator(divide.denominator / gcd);
    return divide;
}


Fraction& Fraction::operator=(const Fraction& tempFraction)
{
    this->setNumerator(tempFraction.getNumerator());
    this->setDenominator(tempFraction.getDenominator());
    return *this;
}

bool Fraction::operator<(const Fraction& tempFraction)
{
    bool flag = false;
    if ((this->numerator * tempFraction.getDenominator()) < (this->denominator * tempFraction.getNumerator()))
        flag = true;
    return flag;
}

bool Fraction::operator>(const Fraction& tempFraction)
{
    bool flag = false;
    if ((this->numerator * tempFraction.getDenominator()) > (this->denominator * tempFraction.getNumerator()))
        flag = true;
    return flag;
}

bool Fraction::operator==(const Fraction& tempFraction)
{
    bool flag = false;
    if ((this->numerator * tempFraction.getDenominator()) == (this->denominator * tempFraction.getNumerator()))
        flag = true;
    return flag;
}

bool Fraction::operator!=(const Fraction& tempFraction)
{
    bool flag = false;
    if ((this->numerator * tempFraction.getDenominator()) != (this->denominator * tempFraction.getNumerator()))
        flag = true;
    return flag;
}

ostream& operator<<(ostream& output, const Fraction& tempFraction)
{
    output << "Numerator:" << tempFraction.numerator << endl;
    output << "\t\t--" << endl;
    output << "Denominator:" << tempFraction.denominator << endl;
    return output;
}

istream& operator>>(istream& input, Fraction& tempFraction)
{
    cout << "Enter the numerator:";
    input >> tempFraction.numerator;
    cout << "Enter the denominator:";
    input >> tempFraction.denominator;
    return input;
}

主要.cpp

#include<iostream>
#include<iomanip>
#include"Fraction.h"

using namespace std;

int main()
{
    Fraction f1, f2, f3;
    int option;
    do
    {
        int exit = 0;
        Fraction f1, f2, f3;
        f1.displayMenu(option);
        switch (option)
        {
        case 1:
            while (exit != -1)
            {
                cin >> f1;
                cin >> f2;
                f3 = f1 + f2;

                cout << f1.getNumerator() << setw(5) << " " << setw(1) << f2.getNumerator() << setw(5) << " " << f3.getNumerator() << endl;
                cout << "--" << setw(1) << " + " << "-- " << setw(4) << " = " << setw(1) << "--" << endl;
                cout << f1.getDenominator() << setw(5) << " " << setw(1) << f2.getDenominator() << setw(5) << " " << f3.getDenominator() << endl;
                f1.exitDisplayMenu(exit);
            }
            break;
        case 2:
            while (exit != -1)
            {
                cin >> f1;
                cin >> f2;
                f3 = f1 - f2;
                cout << f1.getNumerator() << setw(5) << " " << setw(2) << f2.getNumerator() << setw(6) << " " << f3.getNumerator() << endl;
                cout << "-- " << setw(1) << " - " << "-- " << setw(4) << " = " << setw(1) << "--" << endl;
                cout << f1.getDenominator() << setw(5) << " " << setw(2) << f2.getDenominator() << " " << setw(6) << f3.getDenominator() << endl;
                f1.exitDisplayMenu(exit);
            }
            break;
        case 3:
            while (exit != -1) 
            {
                cin >> f1;
                cin >> f2;
                f3 = f1 * f2;
                cout << f1.getNumerator() << setw(5) << " " << setw(2) << f2.getNumerator() << setw(6) << " " << f3.getNumerator() << endl;
                cout << "-- " << setw(1) << " * " << "-- " << setw(4) << " = " << setw(1) << "--" << endl;
                cout << f1.getDenominator() << setw(5) << " " << setw(2) << f2.getDenominator() << " " << setw(6) << f3.getDenominator() << endl;
                f1.exitDisplayMenu(exit);
            }
            break;
        case 4:
            while (exit != -1) 
            {
                cin >> f1;
                cin >> f2;
                f3 = f1 / f2;
                cout << f1.getNumerator() << setw(5) << " " << setw(2) << f2.getNumerator() << setw(6) << " " << f3.getNumerator() << endl;
                cout << "-- " << setw(1) << " / " << "-- " << setw(4) << " = " << setw(1) << "--" << endl;
                cout << f1.getDenominator() << setw(5) << " " << setw(2) << f2.getDenominator() << " " << setw(6) << f3.getDenominator() << endl;
                f1.exitDisplayMenu(exit);
            }
            break;
        case 5:
            while (exit != -1) {
                cin >> f1;
                cin >> f2;
                if (f1 > f2) {
                    cout << f1.getNumerator() << setw(5) << " " << setw(2) << f2.getNumerator() << setw(6) << " " << endl;
                    cout << "-- " << setw(1) << " > " << "-- " << setw(1) << endl;
                    cout << f1.getDenominator() << setw(2) << " " << setw(5) << f2.getDenominator() << " " << setw(6) << endl;
                }
                else if (f1 < f2) {
                    cout << f1.getNumerator() << setw(5) << " " << setw(2) << f2.getNumerator() << setw(6) << " " << endl;
                    cout << "-- " << setw(1) << " < " << "-- " << setw(1) << endl;
                    cout << f1.getDenominator() << setw(2) << " " << setw(5) << f2.getDenominator() << " " << setw(6) << endl;

                }
                else if (f1 == f2) {
                    cout << f1.getNumerator() << setw(5) << " " << setw(2) << f2.getNumerator() << setw(6) << " " << endl;
                    cout << "-- " << setw(1) << " = " << "-- " << setw(1) << endl;
                    cout << f1.getDenominator() << setw(2) << " " << setw(5) << f2.getDenominator() << " " << setw(6) << endl;
                }
                else {
                    cout << f1.getNumerator() << setw(5) << " " << setw(2) << f2.getNumerator() << setw(6) << " " << endl;
                    cout << "-- " << setw(1) << " != " << "-- " << setw(1) << endl;
                    cout << f1.getDenominator() << setw(2) << " " << setw(5) << f2.getDenominator() << " " << setw(6) << endl;

                }
                f1.exitDisplayMenu(exit);
            }
            break;
        case 6: 
            break;
        default:
            cout << "Enter a number from 1-6: ";
            cin >> option;
        }
    } while (option != 6);

    return 0;
}

最佳答案

Fraction& Fraction::operator+(const Fraction& tempFraction) 中:

Fraction& Fraction::operator+(const Fraction& tempFraction)
{
    Fraction sum;
    sum.setNumerator((this->numerator * tempFraction.getDenominator()) + (this->denominator * tempFraction.getNumerator()));
    sum.setDenominator(this->denominator * tempFraction.getDenominator());
    int gcd = GreatestCommonDenominator(sum);
    sum.setNumerator(sum.numerator / gcd);
    sum.setDenominator(sum.denominator / gcd);
    return sum;
}

sum 是局部变量。

...您后来将其通过引用退回。

从不 通过引用返回局部变量。它在函数返回后被销毁,任何对它的引用都变成悬空引用。

您应该返回一个 Fraction 而不是 Fraction&。这同样适用于所有其他 -、*、/ 运算符。


请不要忽略编译器警告。在你的情况下你应该得到一个 C4172:从 MSVC 返回局部变量或临时变量的地址:sum

关于c++运算符重载=在visual studio中运行不佳但在eclipse CDT中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58906489/

相关文章:

c++ - 使用平面数组访问多维数组数据

c# - .net 中 C++ 的包装类?

windows - 如何从 Visual Studio 的上下文菜单中删除元素

c# - 如何在 Visual Studio 输出窗口中运行控制台应用程序,而不是打开新的命令提示符?

c++ - 一个来源有多个对象

c++ - 通过凹多边形计算位置聚类中心约束的最快方法是什么

c++ - 从 Qt 中的 QGridLayout 中删除小部件?

c++ - Getters 和 Setters C++

C#/ Visual Studio : Sorting attributes for consistency - any hints?

c# - 发布时如何以特定目标运行 MsBuild?