Syntax Error: Friending a class

My compiler keeps stating that I have a syntax error when I try to friend ostream. "syntax error: missing ';' before '&' (line 55 on this page) What am I doing wrong?

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
#include <iostream>

#ifndef RATIONALNUMBER_H
#define RATIONALNUMBER_H


class RationalNumber
{
	private:
		int numerator;
		int denominator;
	public:

		//constructor
		RationalNumber(int = 1, int = 1);

		//copy constructor
		RationalNumber(const RationalNumber&);
		RationalNumber(const RationalNumber*);

		//destructor
		~RationalNumber();

		//Predicate Methods
		bool IsValidNumerator(int);
		bool IsValidDenominator(int);

		//Mutators
		void ReduceF();
		double ToDecimal() const
		{ return static_cast<double>(numerator)/denominator; }
		bool SetNumerator(int);
		bool SetDenominator(int);

		//accessors
		int GetNumerator() const
		 { return numerator; }

		int GetDenominator() const
		{ return denominator; }

		//overloading operatorands
		RationalNumber& operator = (const RationalNumber&);
		RationalNumber& operator + (const RationalNumber&);
		RationalNumber& operator - (const RationalNumber&);
		RationalNumber& operator * (const RationalNumber&);
		RationalNumber& operator / (const RationalNumber&);
		bool RationalNumber::operator < (const RationalNumber&);
		bool RationalNumber::operator <= (const RationalNumber&);
		bool RationalNumber::operator > (const RationalNumber&);
		bool RationalNumber::operator >= (const RationalNumber&);
		bool RationalNumber::operator == (const RationalNumber&);
		bool RationalNumber::operator != (const RationalNumber&);
		
		friend ostream& operator << (ostream&, const RationalNumber& );
		friend ostream& operator >> (ostream&, const RationalNumber& );




};


I'm trying to friend ostream so that i can use the operands >> and << to do stream insertion and extraction with fractions.
Last edited on
closed account (o3hC5Di1)
Hi there,

I can see two things:

bool RationalNumber::operator < (const RationalNumber&);

You don't have to qualify the classname within the class.

friend ostream& operator << (ostream&, const RationalNumber& );

I don't see you using using namespace std; anywhere, so you need to fully qualify ostream:

friend std::ostream& operator << (std::ostream&, const RationalNumber& );

All the best,
NwN
I don't see you using using namespace std; anywhere

Which is a good thing - using statements in header files are a bad idea.
Last edited on
Topic archived. No new replies allowed.