Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6530487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:45:41+00:00 2026-05-25T09:45:41+00:00

I have a class called Fraction : #ifndef FRACTION_H #define FRACTION_H #include <iostream> using

  • 0

I have a class called 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

with the next implementation file :

#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;
}

and I’m trying to do the action Fraction f4=f2+2; but I get the following compiler error:

..\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&)

But how could that be , if I have a conversion Constructor (please notice the Ctor in the .h file with the default values) with one argument which is suppose to convert the “2” into a Fraction …then what’s might the be the problem ?

thanks
Ronen

Edit :

here’s the main file (if it would help)

#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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T09:45:41+00:00Added an answer on May 25, 2026 at 9:45 am

    Problem is:

    operator double ()
    

    defined inside your Fraction class.

    While evaluating,

    Fraction f4=f2+2;
    

    Compiler has 2 choices:

    First Choice:

    The compiler can convert f2 to a double using your provided operator function and then it can be used to call the inbuilt operator function:

        operator+(double, int);
    

    Second Choice:

    The compiler can convert 2 to a object of Fraction using constructor and then call:

    const Fraction operator+(const Fraction&, const Fraction&)
    

    The two choices cause an ambiguity and then the compiler complains and reports it to you.

    Solution:
    Change the name of the double operator function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class called Object which stores some data. I would like to
I have an attribute in my car class called VehicleType. Using an enum, I
I have a class called Textures that use holds some data like this //Textures.h
I have a class called fraction, and I'm declaring some operators as friends. I
I have a class called EventConsumer which defines an event EventConsumed and a method
I have a class called Ship and a class called Lifeboat Lifeboat inherits from
I have a class called DatabaseHelper that wraps a DbConnection. What's the proper way
I have this class called Table: class Table { public string Name { get
I have a class called User with static function loginRequired(), which returns false if
I have a class called Book; class Book { public string Name { get;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.