我有一个名为Fraction的课程:
#ifndef FRACTION_H
#define FRACTION_H
#include <iostream>
using namespace std;
class Fraction
{
// data
int m_iNom;
int m_iDenom;
// operations
int gcd (int i, int j);
void reduce ();
public:
Fraction (int nn=0, int dn=1); // 1 declaration = 3 constructors
Fraction (const Fraction& fr); //C.Ctor
~Fraction (); //Dtor
Fraction& operator = (const Fraction &fr); //assignment
Fraction& operator ++ (); // prefix - ++a
const Fraction operator ++ (int); // postfix - a++
friend const Fraction operator + (const Fraction &f1, const Fraction &f2);
friend const Fraction operator - (const Fraction &f1, const Fraction &f2);
friend const Fraction operator * (const Fraction &f1, const Fraction &f2);
friend const Fraction operator / (const Fraction &f1, const Fraction &f2);
Fraction& operator += (const Fraction &f);
operator double () { return double (m_iNom) / m_iDenom; } //casting operator
friend istream& operator >> (istream &is, Fraction &f);
friend ostream& operator << (ostream &os, const Fraction &f);
const int& operator[] (int i) const;
int& operator [] (int i);
};
#endif
使用下一个实现文件:
#include "Fraction.h"
#include <iostream>
using namespace std;
Fraction::Fraction (int nn, int dd) :
m_iNom (nn), m_iDenom (dd) {
if (m_iDenom == 0)
m_iDenom = 1;
reduce ();
cout<<"Ctor - Fraction: "<<m_iNom<<"/"<<m_iDenom<<endl;
}
Fraction::Fraction (const Fraction & fr){
m_iNom=fr.m_iNom;
m_iDenom=fr.m_iDenom;
cout<<"C.Ctor - Fraction: "<<m_iNom<<"/"<<m_iDenom<<endl;
}
Fraction::~Fraction() {
cout<<"del: "<<m_iNom<<"/"<<m_iDenom<<endl;
}
int Fraction::gcd (int i, int j) {
if ((i == 0) || (j == 0))
return i + j;
while (i %= j) {
int t = i;
i = j;
j = t;
}
return j;
}
void Fraction::reduce () {
int g = gcd (m_iNom, m_iDenom);
m_iNom /= g;
m_iDenom /= g;
}
const Fraction operator + (const Fraction &f1, const Fraction &f2) {
int nn = f1.m_iNom * f2.m_iDenom + f1.m_iDenom * f2.m_iNom;
int dd = f1.m_iDenom * f2.m_iDenom;
return Fraction (nn, dd);
}
const Fraction operator - (const Fraction &f1, const Fraction &f2) {
int nn = f1.m_iNom * f2.m_iDenom - f1.m_iDenom * f2.m_iNom;
int dd = f1.m_iDenom * f2.m_iDenom;
return Fraction (nn, dd);
}
const Fraction operator * (const Fraction &f1, const Fraction &f2) {
int nn = f1.m_iNom * f2.m_iNom;
int dd = f1.m_iDenom * f2.m_iDenom;
return Fraction (nn, dd);
}
const Fraction operator / (const Fraction &f1, const Fraction &f2) {
int nn = f1.m_iNom * f2.m_iDenom;
int dd = f1.m_iDenom * f2.m_iNom;
return Fraction (nn, dd);
}
Fraction& Fraction::operator = (const Fraction &f)
{
m_iNom = f.m_iNom;
m_iDenom = f.m_iDenom;
cout<<"OP = - Fraction: "<<m_iNom<<"/"<<m_iDenom<<endl;
return *this;
}
Fraction& Fraction::operator += (const Fraction &f) {
(*this) = (*this) + f;
return *this;
}
Fraction& Fraction::operator ++ ()
{
m_iNom += m_iDenom;
reduce ();
return *this;
}
const Fraction Fraction::operator ++ (int)
{
int nn = m_iNom;
int dd = m_iDenom;
m_iNom += m_iDenom;
reduce ();
return Fraction (nn, dd);
}
istream& operator >> (istream &is, Fraction &frac)
{
char divSign;
is >> frac.m_iNom >> divSign >> frac.m_iDenom;
if (frac.m_iDenom == 0)
frac.m_iDenom = 1;
frac.reduce ();
return is;
}
ostream& operator << (ostream& os, const Fraction &frac)
{
return os << frac.m_iNom << "/" << frac.m_iDenom;
}
int& Fraction::operator [] (int i){
cout<<"reg []"<<endl;
if (i==1)
return m_iNom;
return m_iDenom;
}
const int& Fraction::operator[] (int i) const{
cout<<"const []"<<endl;
if (i==1)
return m_iNom;
return m_iDenom;
}
并且我正在尝试执行 Fraction f4 = f2 + 2; 但我得到以下编译器错误:
..\main.cpp:13: error: ambiguous overload for 'operator+' in 'f2 + 2'
..\main.cpp:13: note: candidates are: operator+(double, int) <built-in>
..\Fraction.h:27: note: const Fraction operator+(const Fraction&, const Fraction&)
但是怎么可能呢,如果我有一个转换构造函数(请注意带有默认值的.h文件中的Ctor)和一个参数,假设将“2”转换为Fraction ...那么什么是可能是问题吗?
感谢 RONEN
编辑:
这是主文件(如果有帮助的话)
#include <iostream>
using namespace std;
#include "Fraction.h"
int main() {
Fraction f1(1,2);
Fraction f2(2);
Fraction f3;
Fraction f4=f2+2; // problem's here
f2+f2;
Fraction f5=f2-f1+f4;
return 0;
}
答案 0 :(得分:3)
编译器无法决定是将f2
转换为double
然后添加double
和int
,还是从2
构建分数然后再添加两个级分。
选项:
答案 1 :(得分:2)
问题是:
operator double ()
在Fraction
班级内定义。
评估时,
Fraction f4=f2+2;
编译器有两个选择:
首选:
编译器可以使用您提供的运算符函数将f2
转换为double
,然后可以使用它来调用内置的运算符函数:
operator+(double, int);
第二选择:
编译器可以使用构造函数将2
转换为Fraction
的对象,然后调用:
const Fraction operator+(const Fraction&, const Fraction&)
这两个选择导致歧义,然后编译器会抱怨并向您报告。
<强>解决方案:强>
更改double
运算符函数的名称。
答案 2 :(得分:1)
当您编写类似f2+2
的内容时,编译器确实可以做出两个选择。
您必须使构造函数显式,或者为双运算符指定显式名称(例如toDouble)以解决问题。