分数运算符重载
fraction operator overloading
我是 C++ 新手,遇到运算符重载问题。我刚刚实现了 header 中的函数 file.I 想为每个函数添加两个最简单的小数形式 other.I 实现了一个 gcd 用于简化并实现了 operator + 用于 addinf the together bu 我得到了一个错误operator+.Error 中的部分在分数 add(f1.getNum1()) f1 中被高亮显示:"no instance of construction (fraction::fraction) matching the argumentlist, types are(int,int)"
这是代码:
a& operator+=(const a& f1,const a& f2){
a add(f1.getNum1()*f2.getDen2()+f2.getNum2()*f1.getDen1(),f1.getDen1()*f2.getDen2());
return add;
}
#endif
我在 main.cpp 代码中遇到另一个问题没有运算符“<<”匹配这些操作数操作数。我初始化 constructor.Now 我想添加小数 numbers.But 结果左侧的 cout“<<”出现错误:result=fractional1+fractional2; cout << num1 <<"/"<< den1 <<"+"<<num2<<"/"<< den2 <<" = "<<result <<endl;
在您的 operator+=
中,您声明了一个分数:
fraction add(f1.getNum1()*f2.getDen2()+f2.getNum2()*f1.getDen1(),f1.getDen1()*f2.getDen2());
这是以下形式:
fraction a(int1, int2);
但是您还没有为 fraction
定义任何接受两个 int
参数的构造函数。编译器告诉你(正确地)它不知道你的意思。
我是 C++ 新手,遇到运算符重载问题。我刚刚实现了 header 中的函数 file.I 想为每个函数添加两个最简单的小数形式 other.I 实现了一个 gcd 用于简化并实现了 operator + 用于 addinf the together bu 我得到了一个错误operator+.Error 中的部分在分数 add(f1.getNum1()) f1 中被高亮显示:"no instance of construction (fraction::fraction) matching the argumentlist, types are(int,int)" 这是代码:
a& operator+=(const a& f1,const a& f2){
a add(f1.getNum1()*f2.getDen2()+f2.getNum2()*f1.getDen1(),f1.getDen1()*f2.getDen2());
return add;
}
#endif
我在 main.cpp 代码中遇到另一个问题没有运算符“<<”匹配这些操作数操作数。我初始化 constructor.Now 我想添加小数 numbers.But 结果左侧的 cout“<<”出现错误:result=fractional1+fractional2; cout << num1 <<"/"<< den1 <<"+"<<num2<<"/"<< den2 <<" = "<<result <<endl;
在您的 operator+=
中,您声明了一个分数:
fraction add(f1.getNum1()*f2.getDen2()+f2.getNum2()*f1.getDen1(),f1.getDen1()*f2.getDen2());
这是以下形式:
fraction a(int1, int2);
但是您还没有为 fraction
定义任何接受两个 int
参数的构造函数。编译器告诉你(正确地)它不知道你的意思。