Need help returning an object from an operator+ and operator* class function.

Here's what i have. It's only partially finished. The int Fraction::operator+ (Fraction &add) and the int Fraction::operator* (Fraction &multi) work right. i have steeped through them line by line and temp in both has the proper values. But I can't get them the print out. Also if i try to do c = a + b; I get an "Error: no operator "=" matches these operands". Some advice would be greatly appreciated.
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Fraction.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

class Fraction
{
private:
	int num, den;
public:
	Fraction ();
	Fraction (int n , int d );
	Fraction (const Fraction&);
	int operator * (Fraction&);
	int operator + (Fraction&);
	void operator = (Fraction&);
	int operator == (Fraction&);
	operator double();
	int getNumerator ();
	int getDenominator ();
	void setNumerator(int);
	void setDenominator (int);
	

};
Fraction ::Fraction()
{
	num = 1;
	den=1;
}
Fraction::Fraction(int n, int d)
{
	num = n;
	den = d;
}
Fraction::Fraction(const Fraction& old)
{
	num = old.num;
	den = old.den;
}
int Fraction::operator* (Fraction &multi)
{
	Fraction temp;
	temp.num = num * multi.num;
	temp.den = den * multi.den;
	return (temp);
}
int Fraction::operator+ (Fraction &add)
{
	Fraction temp;
	temp.num = (num * add.den) + (add.num * den);
	temp.den = den * add.den;
	return(temp);
}
void Fraction::operator= (Fraction &equal)
{
	num = equal.num;
	den = equal.den;
	return;
}
int Fraction::operator== (Fraction &comp)
{
	if (num == comp.num && den == comp.den)
		return (1);
	else
		return(0);
}
Fraction::operator double()
{
	double decimal = 0.0;
	return (decimal);
}
int Fraction::getNumerator()
{
	return(num);
}
int Fraction::getDenominator()
{
	return(den);
}
void Fraction::setNumerator(int x)
{
	num = x;
	return;
}
void Fraction::setDenominator(int y)
{
	den = y;
	return;
}
int main()
{
	int x,y;
	Fraction a,b,c;

	cout << "Enter numerator for 1st fraction: ";
	cin >> x;
	cout << "Enter denamenator for 1st fraction: ";
	cin >> y;
	a.setNumerator(x);
	a.setDenominator(y);
	cout << "Enter numerator for 2st fraction: ";
	cin >> x;
	cout << "Enter denamenator for 2st fraction: ";
	cin >> y;
	b.setNumerator(x);
	b.setDenominator(y);
	c = a + b;
	cout << a.getNumerator() << a.getDenominator() << endl;
	cout << b.getNumerator() << b.getDenominator() << endl;
	cout << c.getNumerator() << c.getDenominator() << endl;
	cout << (a+b) << " " << (a*b) <<  endl;
	system("pause");
	return 0;
}

You have a problem in that a lot of you operators return int when you should really be returning a Fraction

So Fraction = Fraction + Fraction does not work - because Fraction+Fraction returns int.
and Fraction = int is an error - because your assignment operator expects a Fraction not an int.

So operator* and operator+ should return Fraction.

Operator= should really take a const Fraction&
Personally I would make all 4 of your operator overload take a const Fraction&
1
2
3
4
    Fraction  operator * (const Fraction&);
    Fraction  operator + ( const Fraction&);
    void operator = (const Fraction&);
    bool operator == (const Fraction&); //I use bool not int 


Personally, I would have operator == return bool rather than int.
Last edited on
Thanx i'm going to try that when i get a chance.
Now that i can see that it makes perfect since, since i am making my own data type. DOH
Last edited on
Topic archived. No new replies allowed.