Operator overloading,comparing objects

i'm trying to compare 2 fractions using operator overloading
the program crashes when this is called;

this is defination;

bool operator == (const Fraction& f1, Fraction& f2)
{
if (f1==f2)return true;
else return false;
}

this is my calling in the main;

Fraction f1, f2;
cout<<"Enter in the format: 2/4 or 4/9\n";

cout << "enter first fraction: ";
f1.Input();
cout << "enter second fraction: ";
f2.Input();

Fraction result;

result = (f1 == f2);//i think problem lies here.
result.Show();

and this is the prototype of the operator;

friend bool operator == (const Fraction& f1, Fraction& f2);


kindly help me;

thanks in advance;
Please show code for your Fraction class/struct
class Fraction
{
friend Fraction operator+(const Fraction& f1, const Fraction& f2);
friend Fraction operator-(const Fraction& f1, const Fraction& f2);
friend Fraction operator*(const Fraction& f1, const Fraction& f2);
friend Fraction operator/(const Fraction& f1, const Fraction& f2);

friend bool operator != (const Fraction& f1, Fraction& f2);
friend bool operator == (const Fraction& f1, Fraction& f2);

public:
Fraction();
Fraction(int n, int d=1);

void Input(); // input a fraction from keyboard.
void Show() const; // Display a fraction on screen

int GetNumerator() const;
int GetDenominator() const;

bool SetValue(int n, int d=1);
double Evaluate() const; // Return the decimal value of a fraction

private:
int numerator; // may be any integer
int denominator; // should always be positive
};
Please use code tags when posting code.
sorry @mikeyBoy im new to C++ forums.
i'm trying to compare 2 fractions using operator overloading
the program crashes when this is called;

this is defination;

1
2
3
4
5
bool operator == (const Fraction& f1, Fraction& f2)
{
if (f1==f2)return true;
else return false;
}

this is my calling in the main;

1
2
3
4
5
6
7
8
9
10
11
12
Fraction f1, f2;
cout<<"Enter in the format: 2/4 or 4/9\n";

cout << "enter first fraction: ";
f1.Input();
cout << "enter second fraction: ";
f2.Input();

Fraction result;

result = (f1 == f2);//i think problem lies here.
result.Show();

and this is the prototype of the operator;

friend bool operator == (const Fraction& f1, Fraction& f2);


and here is the class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Fraction
{
friend Fraction operator+(const Fraction& f1, const Fraction& f2);
friend Fraction operator-(const Fraction& f1, const Fraction& f2);
friend Fraction operator*(const Fraction& f1, const Fraction& f2);
friend Fraction operator/(const Fraction& f1, const Fraction& f2);

friend bool operator != (const Fraction& f1, Fraction& f2);
friend bool operator == (const Fraction& f1, Fraction& f2);

public:
Fraction();
Fraction(int n, int d=1);	

void Input();	 // input a fraction from keyboard.
void Show() const;	 // Display a fraction on screen

int GetNumerator() const;
int GetDenominator() const;

bool SetValue(int n, int d=1); 
double Evaluate() const;	// Return the decimal value of a fraction

private:
int numerator;	 // may be any integer
int denominator;	 // should always be positive
};

kindly help me;

thanks in advance;
Your operator definition is wrong. You're attempting to do

if (f1==f2)

in the middle of your definition of that "==" operator. This is recursive. In fact, it's infinitely recursive, so the crash is probably happening because you're overfilling the stack.
ahan thanks
Topic archived. No new replies allowed.