加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – error:overloaded’operator <<'必须是二进

发布时间:2020-12-16 05:43:58 所属栏目:百科 来源:网络整理
导读:我知道有很多这样的问题,但我找不到一个适合我的解决方案. 我试图做简单的分数计算器,可以增加或减少任何数量的函数,并将答案写成缩小的分数. 示例:input = 3/2 4 / 8 ,输出= 2 我正在尝试重载操作符,以实现这一点. 所以在程序中,我正在尝试开发输入,由一个
我知道有很多这样的问题,但我找不到一个适合我的解决方案.

我试图做简单的分数计算器,可以增加或减少任何数量的函数,并将答案写成缩小的分数.

示例:input =
3/2 4 /
8
,输出=
2

我正在尝试重载操作符,以实现这一点.

所以在程序中,我正在尝试开发输入,由一个由运算符“或” – “分隔的分数组成.

表达式中的分数是任意的.

以下6行中的每一行都是有效输入表达式的示例:

1/2 + 3/4
1/2 -5/7+3/5
355/113
3    /9-21/    -7
4/7-5/-8
-2/-3+7/5

*我遇到的问题是,当我运行我的程序时,它有一个重载操作错误:error:overloaded’operator<<'必须是二进制运算符(有3个参数)*

/Users/Spicycurryman/Desktop/ECS40/hw1/fraction.cpp:61:22: error: overloaded 'operator<<' must be a binary operator (has 3 parameters)
  ostream& Fraction::operator<<(ostream &os,Fraction& n)
                     ^
/Users/Spicycurryman/Desktop/ECS40/hw1/fraction.cpp:80:22: error: overloaded 'operator>>' must be a binary operator (has 3 parameters)
  istream& Fraction::operator>>(istream &os,Fraction& n)

我不明白为什么这是一个错误.

我的下列代码如下:

CPP文件

#include "Fraction.h"

Fraction::Fraction(int a,int b)
{

}
int Fraction::find_gcd (int n1,int n2) 
{
  int gcd,remainder;

  remainder = n1 % n2; 
  while ( remainder != 0 )
  {
    n1 = n2;
    n2 = remainder; 
    remainder = n1 % n2; 
  } 
  gcd = n2; 

  return (gcd);
}

void Fraction::reduce_fraction(int nump,int denomp) 
{
  this->nump = nump;
  this->denomp = denomp; 
  int gcd;   
  gcd = find_gcd(nump,denomp);
  nump = nump / gcd;
  denomp = denomp / gcd;

    if ((denomp<0 && nump < 0 ))
    {
        denomp*=-1;
        nump*=-1;
    }
    else if (denomp < 0 &&  nump >0){
        denomp*=-1;

    }
    if ( denomp ==0) {
        throw invalid_argument( "Error: zero denominator" );
    }   
}



Fraction& Fraction::operator+(const Fraction& n) {
    denom = denomp * n.denom;
    numera = (nump * n.numera) + (n.denom * n.nump);
    return (*this);

}

Fraction& Fraction::operator-(const Fraction& n) {
    denom = denomp * n.denom;
    numera = (nump * n.numera) - (n.denom* n.nump);
    return (*this);
}

  ostream& Fraction::operator<<(ostream &os,Fraction& n)
{
    if (n.numera == 0)
    {
        cout << 0 << endl;
        return os;
    }
    else if (n.numera == n.denom)
    {
        cout << 1 << endl;
        return os;
    }
    else
    {
        cout << n.numera << '/' << n.denom << endl;
        return os;
    }
}

  istream& Fraction::operator>>(istream &os,Fraction& n)
{
    char slash = 0;
    return os >> n.numera >> slash >> n.denom;

}

标题文件

#ifndef FRACTION_H
#define FRACTION_H
#include <iostream>
#include <stdexcept>
using namespace std;


class Fraction{

    public: 
    Fraction(int a,int b);
    int fraction(int a,int b);
    int find_gcd(int n1,int n2); 
    void reduce_fraction(int nump,int denomp);
    Fraction& operator+(const Fraction& n);
    Fraction& operator-(const Fraction& n);
    friend ostream& operator<<(ostream &os,const  Fraction& n);
    friend istream& operator>>(istream &is,const Fraction& n);
private:
    int denom;
    int numera;
    int denomp;
    int nump;



};

#endif

主CPP文件

#include "Fraction.h"
#include <iostream>
using namespace std;

int main()
{
  Fraction x(2,3);
  Fraction y(6,-2);

  cout << x << endl;
  cout << y << endl;

  cin >> y;
  cout << y << endl;
  Fraction z = x + y;
  cout << x << " + " << y << " = " << z << endl;
}

我知道运算符是成员函数,成员函数采用隐式的第一个参数,这意味着我的运算符现在可以将三个参数固定为非成员函数;然而,这在本程序中将无法正常工作.我该如何解决这个问题,以便程序能运作?

非常感谢你!

解决方法

问题是你宣称操作符>>和运算符<作为非成员函数,但被定义为成员函数. 这应该解决这个问题(但打开另一组问题).所以代替
ostream& Fraction::operator<<(ostream &os,Fraction& n)
  {
     ...


  istream& Fraction::operator>>(istream &os,Fraction& n)
  {
     ...

实施为:

ostream& operator<<(ostream &os,Fraction& n)
{
...

  istream& operator>>(istream &os,Fraction& n)
{
...

另外,请注意你声明的功能是:

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

但定义为(因此您更改了签名):

ostream& Fraction::operator<<(ostream &os,Fraction& n)
  istream& Fraction::operator>>(istream &os,Fraction& n)

正确的方法是声明和定义为:

ostream& Fraction::operator<<(ostream &os,const Fraction& n)
  istream& Fraction::operator>>(istream &os,Fraction& n)

我只是添加更改.其余的与问题相同:

class Fraction{
    friend ostream& operator<<(ostream &os,Fraction& n);
  // the rest is the same
};

ostream& operator<<(ostream &os,const Fraction& n)
{
    if (n.numera == 0)
    {
        cout << 0 << endl;
        return os;
    }
    else if (n.numera == n.denom)
    {
        cout << 1 << endl;
        return os;
    }
    else
    {
        cout << n.numera << '/' << n.denom << endl;
        return os;
    }
}

  istream&  operator>>(istream &os,Fraction& n)
{
    char slash = 0;
    return os >> n.numera >> slash >> n.denom;

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读