C++ 尝试使用重载运算符编写程序时出错

C++ 尝试使用重载运算符编写程序时出错,c++,class,C++,Class,我最近接到一个任务,要求将以前使用类和普通成员函数的现有代码转换为使用重载运算符进行计算的程序。我想我已经知道该怎么做了,但我仍然不熟悉具体的事情,比如获取价值。在上一个构建中,我使用了setter/getter,但在这个构建中,它提供了一个错误,指出“调用'Rational::setnumer(Rational)'时没有匹配的函数”。我不确定如何解决这个问题,或者setter/getter在这种情况下是否可以工作,我的代码如下: using namespace std; class Rati

我最近接到一个任务,要求将以前使用类和普通成员函数的现有代码转换为使用重载运算符进行计算的程序。我想我已经知道该怎么做了,但我仍然不熟悉具体的事情,比如获取价值。在上一个构建中,我使用了setter/getter,但在这个构建中,它提供了一个错误,指出“调用'Rational::setnumer(Rational)'时没有匹配的函数”。我不确定如何解决这个问题,或者setter/getter在这种情况下是否可以工作,我的代码如下:

using namespace std;

class Rational
{
    private:
        int numer;
        int denomer;

    public:
        // Default constructor that initializes objects to 0
        Rational()
        {
            this -> numer = 0;
            this -> denomer = 0;
        }

        // Get member function
        int getnumer()
        {
            return numer;
        }

        // Set member function
        void setnumer(int n)
        {
            this -> numer = n;
        }

        // Get member function
        int getdenomer ()
        {
            return denomer;
        }

        // Set member function
        void setdenomer (int d)
        {
            this -> denomer = d;
        }

        // Constructor to represent rationals as two integers
        Rational(int n, int d)
        {
            this -> numer = n;
            this -> denomer = d;
        }

        // Constructor with single int parameter
        Rational (int whole_number)
        {
            this -> numer = whole_number;
            this -> denomer = 1;
        }

        // Function that prints the sum
        void printNumber()
        {
            cout << getnumer() << "/" << getdenomer();
        }

        // Overload output operator
        friend ostream& operator << (ostream& fout, const Rational r1);

        // Input stream function (not in use since no file to enter info from)
        friend istream& operator >> (istream& fin, const Rational r1);

        // Overload '==' operator
        bool operator == (const Rational& r1);
        bool operator < (const Rational& rl);
        bool operator <= (const Rational& r1);
        bool operator > (const Rational& r1);
        bool operator >= (const Rational& r1);
        Rational operator + (const Rational& r1);
        Rational operator - (const Rational& r1);
        Rational operator * (const Rational& r1);
        Rational operator / (const Rational& r1);


};

int main()
{
    int num1, den1, num2, den2;
    Rational r3;

    // Creates file to put data into
    ofstream fout("Rationals.txt", ios::app);

    // Asks for value inputs
    cout << "Please enter the first numerator value: ";
    cin >> num1;

    cout << "Please enter the first denominator value: ";
    cin >> den1;

    cout << "Please enter the second numerator value: ";
    cin >> num2;

    cout << "Please enter the second denominator value: ";
    cin >> den2;

    // Performs addition calculation
    cout << "\nAddition: ";
    cout << num1 << " / " << den1 << " + " << num2 << " / " << den2 << " = ";
    Rational r1(num1, den1);
    Rational r2 (num2, den2);

    r3 = r1 + r2;
    r3.printNumber();
    fout << r3;

    return 0;
}

istream& operator >> (istream& fin, const Rational r1)
{
    Rational num1, den1, num2, den2;
    fin >> num1 >> den1 >> num2 >> den2;
}

ostream& operator << (ostream& fout, const Rational r1)
{
    Rational r2;
    fout << r2.getnumer() << "/" << r2.getdenomer() << "\n";

    return fout;
}

bool Rational::operator == (const Rational& r1)
{
    Rational a, b, c, d, r2;

    a = getnumer();
    b = getdenomer();
    c = r2.getnumer();
    d = r2.getdenomer();

    return ((a * d) == (c * b));
}

bool Rational::operator < (const Rational& rl)
    {
            Rational a, b, c, d, r2;

            a = getnumer();
            b = getdenomer();
            c = r2.getnumer();
            d = r2.getdenomer();

            return ((a * b) < (c*b));
    }

bool Rational::operator <= (const Rational& r1)
{
    Rational a, b, c, d, r2;

    a = getnumer();
    b = getdenomer();
    c = r2.getnumer();
    d = r2.getdenomer();

    return ((a * b) <= (c*b));
}

bool Rational::operator > (const Rational& r1)
{
    Rational a, b, c, d, r2;

    a = getnumer();
    b = getdenomer();
    c = r2.getnumer();
    d = r2.getdenomer();

    return ((a * b) > (c*b));
}

bool Rational::operator >= (const Rational& r1)
{
    Rational a, b, c, d, r2;

    a = getnumer();
    b = getdenomer();
    c = r2.getnumer();
    d = r2.getdenomer();

    return ((a * b) >= (c*b));
}


Rational Rational::operator + (const Rational& r1)
{
    Rational a, b, c, d, r, r2;

    a = getnumer();
    b = getdenomer();
    c = r.getnumer();
    d = r.getdenomer();

    r2.setnumer((a * d) + (b * c));
    r2.setdenomer(b * d);

    return r2;
}

Rational Rational::operator - (const Rational& r1)
{
    Rational a, b, c, d, r, r2;

    a = getnumer();
    b = getdenomer();
    c = r1.getnumer();
    d = r1.getdenomer();

    r2.setnumer(a * d - b * c);
    r2.setdenomer(b * d);

    return r2;
}

Rational Rational::operator * (const Rational& r1)
{
    Rational a, b, c, d, r, r2;

    a = getnumer();
    b = getdenomer();
    c = r.getnumer();
    d = r.getdenomer();

    r2.setnumer(a * c);
    r2.setdenomer(b * d);

    return r2;
}

Rational Rational::operator / (const Rational& r1)
{
    Rational a, b, c, d, r, r2;

    a = getnumer();
    b = getdenomer();
    c = r1.getnumer();
    d = r1.getdenomer();

    r2.setnumer(a * d);
    r2.setdenomer(c * b);

    return r2;
}
使用名称空间std;
类理性
{
私人:
整数;
int去噪器;
公众:
//将对象初始化为0的默认构造函数
Rational()
{
该->数值=0;
该->去噪器=0;
}
//获取成员函数
int getnumer()
{
返回编号;
}
//集合成员函数
无效设置编号(整数n)
{
该->数值=n;
}
//获取成员函数
int getdenomer()
{
返回去噪器;
}
//集合成员函数
void setdenomer(int d)
{
这个->去噪器=d;
}
//构造函数将有理数表示为两个整数
有理数(整数n,整数d)
{
该->数值=n;
这个->去噪器=d;
}
//具有单个int参数的构造函数
有理数(整数)
{
此->数字=整数;
这个->去噪器=1;
}
//用于打印和的函数
无效打印编号()
{
coutnum1;
cout>den1;
cout>num2;
cout>den2;
//执行加法计算
cout-den2;
}

ostream&operator这里有几个错误

首先,您忘记包含几个标题:

#include <iostream>
#include <fstream>
第四,将int赋值给有理数。例如:

bool Rational::operator >= (const Rational& r1)
{
    Rational a, b, c, d, r2;

    a = getnumer(); //a is a Rational, not an int
    b = getdenomer(); //same here
    c = r2.getnumer(); //same here, and r2 is the default, don't you mean r1?
    d = r2.getdenomer(); //same here, and r2 is the default, don't you mean r1?

    return ((a * b) >= (c*b));
}
bool Rational::operator >= (const Rational& r1)
{
    Rational a, b, c, d, r2;

    a = getnumer(); //getnumer and getdenomer are not const fucntions, and are being used on a const object!
    b = getdenomer(); //same here
    c = r2.getnumer(); //same here
    d = r2.getdenomer(); //same here

    return ((a * b) >= (c*b));
}
最后,在
const
对象上使用非
const
函数。示例:

bool Rational::operator >= (const Rational& r1)
{
    Rational a, b, c, d, r2;

    a = getnumer(); //a is a Rational, not an int
    b = getdenomer(); //same here
    c = r2.getnumer(); //same here, and r2 is the default, don't you mean r1?
    d = r2.getdenomer(); //same here, and r2 is the default, don't you mean r1?

    return ((a * b) >= (c*b));
}
bool Rational::operator >= (const Rational& r1)
{
    Rational a, b, c, d, r2;

    a = getnumer(); //getnumer and getdenomer are not const fucntions, and are being used on a const object!
    b = getdenomer(); //same here
    c = r2.getnumer(); //same here
    d = r2.getdenomer(); //same here

    return ((a * b) >= (c*b));
}
这是固定代码:


#include <iostream> //Remember to include this for the streams
#include <fstream> //for the filestream

using namespace std;

class Rational
{
private:
    int numer;
    int denomer;

public:
    // Default constructor that initializes objects to 0
    Rational()
    {
        this->numer = 0;
        this->denomer = 0;
    }

    // Get member function
    int getnumer() const //so we know it does not impact a const object
    {
        return numer;
    }

    // Set member function
    void setnumer(int n)
    {
        this->numer = n;
    }

    // Get member function
    int getdenomer() const //so we know it does not impact a const object
    {
        return denomer;
    }

    // Set member function
    void setdenomer(int d)
    {
        this->denomer = d;
    }

    // Constructor to represent rationals as two integers
    Rational(int n, int d)
    {
        this->numer = n;
        this->denomer = d;
    }

    // Constructor with single int parameter
    Rational(int whole_number)
    {
        this->numer = whole_number;
        this->denomer = 1;
    }

    // Function that prints the sum
    void printNumber()
    {
        cout << getnumer() << "/" << getdenomer();
    }

    // Overload output operator
    friend ostream& operator << (ostream& fout, const Rational r1);

    // Input stream function (not in use since no file to enter info from)
    friend istream& operator >> (istream& fin, const Rational r1);

    // Overload '==' operator
    bool operator == (const Rational& r1);
    bool operator < (const Rational& rl);
    bool operator <= (const Rational& r1);
    bool operator > (const Rational& r1);
    bool operator >= (const Rational& r1);
    Rational operator + (const Rational& r1);
    Rational operator - (const Rational& r1);
    Rational operator * (const Rational& r1);
    Rational operator / (const Rational& r1);


};

istream& operator >> (istream& fin, const Rational r1)
{
    fin >> r1.numer >> r1.denomer; //note the non recursive-ness
    return fin; //note the returning
}

ostream& operator << (ostream& fout, const Rational r1)
{
    Rational r2;
    fout << r2.getnumer() << "/" << r2.getdenomer() << "\n";
    return fout; //note the returning
}

Rational Rational::operator*(const Rational& r) {
    return Rational(this->numer * r.numer, this->denomer * r.denomer); //simplified, does not reduce
}

Rational Rational::operator/(const Rational& r) {
    return Rational(this->numer / r.numer, this->denomer / r.denomer); //simplified, does not reduce
}

Rational Rational::operator-(const Rational& r) {
    if (this->denomer == r.denomer) return Rational(this->numer - r.numer, this->denomer);
    else return Rational((this->numer * r.denomer) - (r.numer * this->denomer), this->denomer * r.denomer);
}

Rational Rational::operator+(const Rational& r) {
    if (this->denomer == r.denomer) return Rational(this->numer + r.numer, this->denomer);
    else return Rational((this->numer * r.denomer) + (r.numer * this->denomer), this->denomer * r.denomer);
}

bool Rational::operator == (const Rational& r1)
{
    int a, b, c, d; //int, not rational

    a = getnumer();
    b = getdenomer();
    c = r1.getnumer();
    d = r1.getdenomer();

    return ((a * d) == (c * b));
}

bool Rational::operator < (const Rational& r1)
{
    int a, b, c, d;

    a = getnumer();
    b = getdenomer();
    c = r1.getnumer();
    d = r1.getdenomer();

    return ((a * b) < (c * b));
}

bool Rational::operator <= (const Rational& r1)
{
    int a, b, c, d; //int, not rational

    a = getnumer();
    b = getdenomer();
    c = r1.getnumer();
    d = r1.getdenomer();

    return ((a * b) <= (c * b));
}

bool Rational::operator > (const Rational& r1)
{
    int a, b, c, d; //int, not rational

    a = getnumer();
    b = getdenomer();
    c = r1.getnumer();
    d = r1.getdenomer();

    return ((a * b) > (c * b));
}

bool Rational::operator >= (const Rational& r1)
{
    int a, b, c, d; //int, not rational

    a = getnumer();
    b = getdenomer();
    c = r1.getnumer();
    d = r1.getdenomer();

    return ((a * b) >= (c * b));
}

#include//记住要将其包含在流中
#文件流的include//
使用名称空间std;
类理性
{
私人:
整数;
int去噪器;
公众:
//将对象初始化为0的默认构造函数
Rational()
{
该->数值=0;
该->去噪器=0;
}
//获取成员函数
int getnumer()const//因此我们知道它不会影响const对象
{
返回编号;
}
//集合成员函数
无效设置编号(整数n)
{
该->数值=n;
}
//获取成员函数
int getdenomer()const//因此我们知道它不会影响const对象
{
返回去噪器;
}
//集合成员函数
void setdenomer(int d)
{
这个->去噪器=d;
}
//构造函数将有理数表示为两个整数
有理数(整数n,整数d)
{
该->数值=n;
这个->去噪器=d;
}
//具有单个int参数的构造函数
有理数(整数)
{
此->数字=整数;
这个->去噪器=1;
}
//用于打印和的函数
无效打印编号()
{
cout>(istream&fin,const Rational r1)
{
fin>>r1.numer>>r1.denomer;//注意非递归性
return fin;//注意返回的
}
ostream&操作符denomer==r.denomer)返回Rational(this->numer-r.numer,this->denomer);
否则返回Rational((this->numer*r.denomer)-(r.numer*this->denomer),this->denomer*r.denomer);
}
Rational::operator+(constrational&r){
如果(this->denomer==r.denomer)返回Rational(this->numer+r.numer,this->denomer);
否则返回Rational((this->numer*r.denomer)+(r.numer*this->denomer),this->denomer*r.denomer);
}
bool Rational::operator==(const Rational&r1)
{
int a,b,c,d;//int,非有理
a=getnumer();
b=getdenomer();
c=r1.getnumer();
d=r1.getdenomer();
返回((a*d)=(c*b));
}
布尔有理::运算符<(constrational&r1)
{
INTA、b、c、d;
a=getnumer();
b=getdenomer();
c=r1.getnumer();
d=r1.getdenomer();
报税表((a*b)<(c*b));
}
布尔有理::算子(c*b));
}
布尔有理::运算符>=(constrational&r1)
{
int a,b,c,d;//int,非有理
a=getnumer();
b=getdenomer();
c=r1.getnumer();
d=r1.getdenomer();
返回((a*b)>=(c*b));
}
现在应该可以了