error:overloaded'operator<<'必须是二进制运算符(有3个参数) [英] error: overloaded 'operator<<' must be a binary operator (has 3 parameters)
问题描述
我知道有很多这样的问题,但我找不到一个适合我的解决方案。
我试图使简单分数计算器比可以添加或减去任何数量的函数,并将答案写为减少的分数。
示例:input =
3/2 + 4 /
8
,output =
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
*我遇到的问题是,当我运行我的程序,它有一个重载操作错误:错误:重载的操作符<<'必须是二进制运算符(有3个参数)*
/Users/Spicycurryman/Desktop/ECS40/hw1/fraction.cpp:61:22:error:overloaded 'operator'<''必须是二进制运算符(具有3个参数)
ostream& Fraction :: operator<<<(ostream& os,Fraction& n)
^
/Users/Spicycurryman/Desktop/ECS40/hw1/fraction.cpp:80:22:error:overloaded'operator> ;>'必须是二进制运算符(具有3个参数)
istream& Fraction :: operator>>(istream& os,Fraction& n)
了解为什么这是一个错误。
我的下列代码如下:
CPP FILE b
$ b
#includeFraction.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& amp; nump< 0))
{
denomp * = - 1;
nump * = - 1;
}
else if(denomp< 0& amp; nump> 0){
denomp * = - 1;
}
if(denomp == 0){
throw invalid_argument(Error:zero denominator);
}
}
分数& Fraction :: operator +(const Fraction& n){
denom = denomp * n.denom;
numera =(nump * n.numera)+(n.denom * n.nump);
return(* this);
}
分数& 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>>斜线>> ;
}
标题文件 >
#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
MAIN CPP FILE / p>
#includeFraction.h
#include< iostream>
using namespace std;
int main()
{
Fraction x(2,3);
分数y(6,-2);
cout<< x < endl;
cout<< y < endl;
cin>> y;
cout<< y < endl;
Fraction z = x + y;
cout<< x < +<< y < =<< z < endl;
}
我知道运算符是成员函数,参数,意味着我的运算符现在需要三个参数,它可以被固定为非成员函数;然而,这不会在这个程序中工作。在我的情况下,我将如何解决它所以程序将工作?
非常感谢!
问题是作为非成员函数声明 operator>>
和运算符<
,但定义为成员函数。 / p>
这应该解决这个问题(但打开另一组问题)。因此,而不是
ostream& Fraction :: operator<<(ostream& os,Fraction& n)
{
...
istream& Fraction :: operator>>(istream& os,Fraction& n)
{
...
$ b b
实现为:
ostream&运算符<<(ostream& os,Fraction& n)
{
...
istream& operator>>(istream& os,Fraction& n)
{
...
此外,请注意您声明的函数为:
朋友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,const Fraction& n);
friend istream& operator>>(istream& is,Fraction& n);
//其余的是相同的
};
ostream&运算符<<<(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&运算符>>(istream& os,Fraction& n)
{
char slash = 0;
return os>> n.numera>>斜线>> ;
}
I know there are plenty of questions like these, but I couldn't find a solution that worked for me.
I am trying to make simple fraction calculator than can add or subtract any number of functions and write the answer as a reduced fraction.
Example: input= 3/2 + 4/ 8 , output = 2
I am trying overload operators in order to accomplish this.
So in the program, I am trying to develop the input consists of an expression made of fractions separated by the operators '+'or '-'.
The number of fractions in the expression is arbitrary.
Each of the following 6 lines is an example of valid input expression:
1/2 + 3/4
1/2 -5/7+3/5
355/113
3 /9-21/ -7
4/7-5/-8
-2/-3+7/5
*The problem that I am having is that in when I run my program it has a overload operating error: error: overloaded 'operator<<' must be a binary operator (has 3 parameters)*
/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)
I don't understand why that is an error.
My following code is below:
CPP FILE
#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;
}
Header File
#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
MAIN CPP FILE
#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;
}
I know that the operators are member functions and a member function takes an implicit first parameter, meaning my operators now takes three parameters it may be fixed being a non-member function; however, that would not work in this program. How exactly in my case would I fix it so the program would work?
Thank you very much!
The problem is that you declared operator>>
and operator<<
as non-member functions, but defined as a member function.
This should fix that problem (but open another set of problems). So instead of
ostream& Fraction::operator<<(ostream &os, Fraction& n)
{
...
istream& Fraction::operator>>(istream &os, Fraction& n)
{
...
implement as :
ostream& operator<<(ostream &os, Fraction& n)
{
...
istream& operator>>(istream &os, Fraction& n)
{
...
Also, take a note that you declared functions as :
friend ostream& operator<<(ostream &os, const Fraction& n);
friend istream& operator>>(istream &is, const Fraction& n);
but defined as (therefore you changed the signature) :
ostream& Fraction::operator<<(ostream &os, Fraction& n)
istream& Fraction::operator>>(istream &os, Fraction& n)
Proper way is to declare and define as :
ostream& Fraction::operator<<(ostream &os, const Fraction& n)
istream& Fraction::operator>>(istream &os, Fraction& n)
I am adding just changes. The rest is the same as in the question:
class Fraction{
friend ostream& operator<<(ostream &os, const Fraction& n);
friend istream& operator>>(istream &is, 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;
}
这篇关于error:overloaded'operator<<'必须是二进制运算符(有3个参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!