Multiple File Compilation Problem

I have this multiple file compilation program and for some reason my overloaded object in my rational.cpp file cant access the private function GCD from the class even though it is a friend of the function. Any help 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//main.cpp:

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include "rational.h"
using namespace std;

int main()
{
	rational r1(1,3), r2(1,3), r3;
	
	cout << "r1 = " << r1;
	cout << endl;
	
	
	cout << "r2 = " << r2;
	cout << endl;
	
	r3 = r1 + r2;
	cout << "r3 = r1 + r2 = " << r3;
	cout << endl;
	
	r3 = r1 - r2;
	cout << "r4 = r1 - r2 = " << r3;
	cout << endl;
	
	r3 = r1 * r2;
	cout << "r5 = r1 * r2 = " << r3;
	cout << endl;
	
	r3 = r1 / r2;
	cout << "r6 = r1 / r2 = " << r3;
	cout << endl;
	
	int com = r1 > r2;
	
	if (com == 1)
		cout << "r1 is greater than r2" << endl;
	else if (com == 0)
		cout << "r1 and r2 are equal" << endl;
	else if (com == -1)
		cout << "r1 is less than r2" << endl;
	
	system ("PAUSE");
	return 0;	
}

//rational.h:

#ifndef RATIONAL_H
#define RATIONAL_H
#include <iostream>
#include <iomanip>
using namespace std;

class rational
{
	friend istream& operator>>(istream&, rational&);
	// User is prompted to enter numerator then denominator for a rational number
	//  Precondition: denominator of rational number must not be zero
	// Postcondition: rational reference object passed to the function is filled with values entered by user

	friend ostream& operator<<(ostream&, rational&);
	// Postcondition: displays the contents of the rational object passed to the function 
	//   Note: the rational number should be displayed in the format of  a / b, e.g., 1 / 2,  -5 /9 (not 5 / -9), 1 / 4 (not 2 / 8), etc.
	//   Moreover, if the rational number is 1 / 1, then display just 1. If the rational number is 0 /5, then display just 0.

public:
	rational();
	// default constructor
	// Postcondition: declared rational object is initialized to 1 (i.e., 1 / 1)

	rational(int aa, int bb);
	// second constructor
	// Postcondition: numerator & denominator of the declared rational object is initialized to aa and bb, respectively and bb ? 0

	void set(int aa, int bb);
	// Postcondition: calling rational object is set to aa / bb

	rational operator+(const rational &r2) const;
	// Postcondition: sum of calling rational object and r2 is returned (notice the return type is rational!)

	rational operator-(const rational &r2) const;
	// Postcondition: (calling rational object – r2) is returned 

	rational operator*(const rational &r2) const;
	// Postcondition: product of calling rational object and r2 is returned 

	rational operator/(const rational &r2) const;
	// Postcondition: (calling rational object / r2) is returned

	int operator>(const rational&r2) const;
	// Postcondition: returns 1 if calling object is greater than r2; 0 if r1 is equal to r2; -1 is r1 is less than r2 

private:
	int GCD() const;
	// You must use the Euclidean algorithm. https://en.wikipedia.org/wiki/Euclidean_algorithm
	// Postcondition: returns the “greatest common divisor” between the numerator and denominator of the calling rational object
	int a;	// numerator
	int b;	// denominator
};

#endif


//rational.cpp:

#include <stdlib.h>
#include "rational.h"
using namespace std;

rational::rational()
{
}

rational::rational(int aa, int bb)
{
	a = aa;
	b = bb;
	
	if (b == 0)
	{
		cout << "Denominator cannot equal 0\n";
		
		exit(1);
	}	
}

void rational::set(int aa, int bb)
{
	cout << "Enter numerator and denominator: \n";
	cin >> aa >> bb;
}

rational rational::operator+(const rational &r2) const
{	
	rational sum;
	sum.a = a * r2.b + r2.a * b;
	sum.b = b * r2.b;
	
	return sum;
}

rational rational::operator-(const rational &r2) const
{
	rational difference;
	difference.a = a * r2.b - r2.a * b;
	difference.b = b * r2.b;
	
	return difference;
}

rational rational::operator*(const rational &r2) const
{
	rational product;
	product.a = a * r2.a;
	product.b = b * r2.b;
	
	return product;
}

rational rational::operator/(const rational &r2) const
{
	rational quotient;
	quotient.a = a * r2.b;
	quotient.b = b * r2.a;
	
	return quotient;
}

int rational::operator>(const rational&r2) const
{
	double r1total, r2total;
	
	r1total = (double) a/b;
	r2total = (double) r2.a/r2.b;
	
	if (r1total > r2total)
		return 1;
	else if (r1total == r2total)
		return 0;
	else 
		return -1;
}

int rational::GCD() const
{
	int n = abs(a);
	int d = abs(b);
	
	while(d != 0)
	{
		int temp = d;
		d = n % d;
		n = temp;
	}
	
	return n;
}

istream& operator>>(istream &in, rational &r2)
{
	in >> r2.a >> r2.b;
	
	return in;
}

ostream& operator<<(ostream &out, rational &r2)
{
	int gcd = GCD();
	
	if (r2.b < 0)
	{
		r2.b = -(r2.b);
		r2.a = -(r2.a);
	}
	
	if(r2.a == 0)
	{
		out << 0;
	}
	
	if(r2.a == r2.b)
	{
		out << 1;
	}
	
	out << r2.a/gcd  << "/" << r2.b/gcd;
	
	return out;
}
> I have this multiple file compilation program and for some reason my
> overloaded object in my rational.cpp file cant access the private function
> GCD from the class even though it is a friend of the function.
overloaded operator
the function is a friend of the class

Not a problem with saying what you understood of the error, but always copy it verbatim.


|| foo.cpp: In function ‘std::ostream& operator<<(std::ostream&, rational&)’:
foo.cpp|211 col 16| error: ‘GCD’ was not declared in this scope
||   int gcd = GCD();
||                 ^
you have declared a non-static member function called GCD().
Because it is a member function you need an object to call it int gcd = r2.GCD();
Topic archived. No new replies allowed.