Operator overloading

Hi below is an example from the book on overloading with the use of return*this;
but i'm getting an error message when i try to compile it to see how it works.

"[Error] definition of implicitly-declared 'Rational& Rational::operator=(const Rational&)'"

normally i'm able to trouble shoot errors i get from the book examples on my own which has help me learn when creating my own code. But i don't understand this concept enough to trouble shoot this without any assistance.

any clarification would be greatly appreciated. thanks

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
  //overloading
//Rational class w/ overloaded = operator
//Project Target Type: Application - Target Model: Console
#include <iostream>
using namespace std;

class Rational
{
	public:
		
		Rational (int = 0, int = 1);
		
		Rational (const Rational&);
		
		void print();
		
	private:
		
		int num, den;
		
		int gcd (int j, int k)
		{
			if (k==0)
			return j;
			return gcd(k,j%k);
		}
		
		void reduce () 
		{
			int g = gcd(num, den); 
			num /= g;
			den /= g;
		}
};

int main()
{
	
	Rational x(100,360);
	Rational y(x);
	Rational z, w;
	
	cout << "x= "; x.print();
	cout << "\ny= "; y.print();
	cout << "\nz= "; z.print();
	
	w = z = y;
	
	cout << "\nz= "; z.print();
	cout << "\nw= "; w.print();
	
	cout << "\n\n\nPress any key to close console window: ";
	
	char c;
	cin >> c;
	
	return 0;
	
}

Rational::Rational(int n, int d) : num (n), den (d)
{
	reduce();
}

Rational::Rational(const Rational& r) : num(r.num), den(r.den)
{
}

void Rational::print()
{
	cout << num << '/' << den;
}

Rational& Rational::operator=(const Rational& r)
{
	num = r.num;
	den = r.den;
	return *this;
}
You have to prototype the function in your class definition.
That was exactly the issue, thanks.
Topic archived. No new replies allowed.