overloading insertion operator

I've created an object called Rational Number and I want to overload the insertion (<<) and extraction (>>) operator so I can output the Rational Number object I'm working with. I created a friend function operator<< that has access to all members of my class. I made the return value ostream, because I want it to work with all ostreams whether it be standard streams, file streams, and in-memory streams. However when I try to compile my code I get the following error message.

ISO C++ forbids declaration of 'ostream' with no type
'ostream' is neither function nor member function; cannot be declared friend

the same error message occurs for operator>> (This is the friend function that deals with extraction for Rational Numbers)

Could someone please explain to me why I am getting these compilation errors and how to fix them? Thank you. Also it is saying the return type in my main program should be int. Why can't I use void?


Here is my code:

/*
* Rational_Test_Driver.cpp
*
*
* Created by Matthew Dunson on 9/18/09.
* Copyright 2009 The Ohio State University. All rights reserved.
*
*/

#include <iostream>
#include <fstream>
#include <cstdlib> // for absolute values

class RationalNum {
//friend function declarations
friend RationalNum operator+(RationalNum, RationalNum);
friend ostream& operator<<(ostream&, RationalNum&); // I get the error here!
friend istream& operator>>(istream&, RationalNum&); // I get the error here!

private:
// data members
int numer;
int denom;

public:
// member functions
RationalNum(); // default constructor
RationalNum(int); // constructor takes one int
RationalNum(int, int); // constructor
int getNumer(); // returns numer value
int getDenom(); // returns denom value
void setRationalNum(int n, int d);
void simplify (); // simplifies the fraction
}; // end of class RationalNum

// member function definitions
RationalNum::RationalNum() { numer = 0; denom = 1; }
RationalNum::RationalNum(int n) { numer = n; denom = 1; }
RationalNum::RationalNum(int n, int d) { numer = n; denom = d; }


int RationalNum::getNumer(){
return numer;
} // end getNumer



int RationalNum::getDenom() {
return denom;
} // end getDenom


void RationalNum::setRationalNum(int n, int d) {
numer = n; denom = d;
} // end setRationalNum


void RationalNum::simplify () {
int p = abs(numer); // absolute value
int q = abs(denom); // absolute value
int small, large, remainder;

if (p >= q) {
large = p;
small = q; }
else {
large = q;
small = p; }

while ( large % small > 0) {
remainder = large % small;
large = small;
small = remainder;
}
numer = numer/small;
denom = denom/small;
} // end function simplify

// friend function definitions



RationalNum operator+(RationalNum lhs, RationalNum rhs) {
int numerator = (lhs.numer * rhs.denom) + (rhs.numer * lhs.denom);
int denominator = lhs.numer * rhs.denom;
RationalNum r(numerator, denominator);
r.simplify();
return r; } // end operator+ function




ostream& operator<<(ostream &out, RationalNum &ob) {
out << ob.numer << " / " << ob.denom;
return out; } // end function operator<<




istream& operator>>(istream &in, RationalNum &ob) {
char divisionSign;
in >> ob.numer >> divisionSign >> ob.denom;
return in; } //end function operator >>






void main () {
ofstream fout("output.dat");

cout << "How many rational numbers for input?";
int arraySize;
cin >> arraySize;

RationalNum *ptr = new RationalNum[arraySize];

int i;
cout << "For each rational type numerator/denominator <enter>\n";

for (i = 0; i < arraySize; i++)
{
cout << "Input rational number " << i+1 << ": ";
cin >> ptr[i];
}

fout << "The " << arraySize << " input rationals are:" << endl;

for ( i = 0; i < arraySize; i++)
fout << ptr[i] << endl;

RationalNum sum;
for (i = 0; i < arraySize; i++)
sum = sum + ptr[i];

fout << "The sum of input rationals = " << sum << endl;

delete [] ptr; // returns dynamic memory to freestore
} // end main function
Last edited on
You need to use the std:: prefix

std::ostream
std::istream

main() should return int.
Thanks,
That did help me compile; however, now I have a runtime error. The program will ask me "How many rational numbers for input?" and I will type in 5 and press enter. Then immediately the code "For each rational type numerator/denominator <enter>" gets executed.
Then the for loop goes asking for my input and storing it into that pointer array. Then it executes the for loop to output the rational numbers into the file "output.dat." However, after doing that the following error occurs:

Floating point exception

Why?
Last edited on
One problem is in your operator+, in computing the resulting denominator.
The resulting denominator is the product of both denominators, which isn't
what you have.

I don't know if this is causing the crash, but it is a problem nonetheless.

If it still crashes, I suggest adding some prints to your simplify() function
and see what it is doing.
Topic archived. No new replies allowed.