Overloading Input Operator
Nov 13, 2014 at 1:16am UTC
I cannot get my function overloading the input operator for rational type objects to work. Can anyone help?
lab9.cpp: In function ‘std::istream& operator>>(std::istream&, const rational&)’:
lab9.cpp:186:20: error: invalid initialization of reference of type ‘std::istream& {aka std::basic_istream<char>&}’ from expression of type ‘rational’
return (inputFrac);
lab9.h
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
#include <iostream>
using namespace std;
class rational
{
public :
rational();
rational(int a, int b);
int getNumerator() {return numerator;};
int getDenomnator() {return denominator;};
rational operator +(const rational &);
rational operator -(rational);
rational operator *(rational);
rational operator /(rational);
bool operator !=(rational);
bool operator ==(rational);
bool operator >(rational);
bool operator <(rational);
bool operator >=(rational);
bool operator <=(rational);
rational operator -();
friend ostream& operator <<(ostream& outs, const rational& frac);
friend istream& operator >>(istream& ins, const rational& frac);
private :
int numerator;
int denominator;
};
lab9main.cpp
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
#include <iostream>
#include "lab9.h"
using namespace std;
int main(){
string op;
rational a, b;
cout << "Let's start to test operators defined on rational class." << endl;
cout << "Enter the operator (+, -, *, /, == .... enter -1 for negation):" << endl;
cin >> op;
if (op == "-1" )
cout << "Enter the operand." << endl;
else
cout << "Enter the two operands." << endl;
cin >> a >> b;
return 0;
}
lab9.cpp
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
#include <iostream>
#include <assert.h>
#include "lab9.h"
using namespace std;
rational::rational()
{
numerator = 0;
denominator = 0;
}
rational::rational(int a, int b)
{
numerator = a;
denominator = b;
}
rational rational::operator +(const rational & frac)
{
rational addFrac;
int temp1 = numerator, temp2 = denominator;
int temp3 = frac.numerator, temp4 = frac.denominator;
temp1 *= frac.denominator;
temp2 *= frac.denominator;
temp3 *= denominator;
temp4 *= denominator;
addFrac.numerator = temp1 + temp3;
assert(temp4 == temp2);
addFrac.denominator = temp2;
return (addFrac);
}
rational rational::operator -(rational frac)
{
rational subFrac;
int temp1 = numerator, temp2 = denominator;
int temp3 = frac.numerator, temp4 = frac.denominator;
temp1 *= frac.denominator;
temp2 *= frac.denominator;
temp3 *= denominator;
temp4 *= denominator;
subFrac.numerator = temp1 - temp3;
assert(temp4 == temp2);
subFrac.denominator = temp2;
return (subFrac);
}
rational rational::operator *(rational frac)
{
rational timesFrac;
timesFrac.numerator = numerator * frac.numerator;
timesFrac.denominator = denominator * frac.denominator;
return (timesFrac);
}
rational rational::operator /(rational frac)
{
rational divFrac;
divFrac.numerator = numerator * frac.denominator;
divFrac.denominator = denominator * frac.numerator;
return (divFrac);
}
bool rational::operator !=(rational frac)
{
if ((numerator * frac.denominator) == (denominator * frac.numerator))
return true ;
else
return false ;
}
bool rational::operator ==(rational frac)
{
if ((numerator * frac.denominator) == (denominator * frac.numerator))
return false ;
else
return true ;
}
bool rational::operator >(rational frac)
{
double (numerator);
double (denominator);
double (frac.numerator);
double (frac.denominator);
if ((numerator / denominator) > (frac.numerator / frac.denominator))
return true ;
else
return false ;
}
bool rational::operator <(rational frac)
{
double (numerator);
double (denominator);
double (frac.numerator);
double (frac.denominator);
if ((numerator / denominator) < (frac.numerator / frac.denominator))
return true ;
else
return false ;
}
bool rational::operator >=(rational frac)
{
double (numerator);
double (denominator);
double (frac.numerator);
double (frac.denominator);
if ((numerator / denominator) > (frac.numerator / frac.denominator))
return true ;
else if ((numerator / denominator) < (frac.numerator / frac.denominator))
return false ;
else
return true ;
}
bool rational::operator <=(rational frac)
{
double (numerator);
double (denominator);
double (frac.numerator);
double (frac.denominator);
if ((numerator / denominator) < (frac.numerator / frac.denominator))
return true ;
else if ((numerator / denominator) > (frac.numerator / frac.denominator))
return false ;
else
return true ;
}
rational rational::operator -()
{
rational negativeFrac;
negativeFrac.numerator = -1 * numerator;
return (negativeFrac);
}
ostream& operator <<(ostream& outs, const rational& frac)
{
outs << frac.numerator << "/" << frac.denominator << endl;
}
istream& operator >>(istream& ins, const rational& frac)
{
rational inputFrac;
char slash;
ins >> inputFrac.numerator;
do
ins >> slash;
while (slash != '/' );
ins >> inputFrac.denominator;
return (inputFrac);
}
Nov 13, 2014 at 1:25am UTC
Your function should be returning an istream object not a rational object
Nov 13, 2014 at 1:33am UTC
I don't really understand streams. Can you please tell me what I should return if I want to use the operator to input an object that represents a fraction as I have thus far done in this program?
Nov 13, 2014 at 2:03am UTC
1 2 3 4 5 6 7 8
istream& operator >>(istream& ins, rational& frac)
{
ins >> frac.numerator;
while (ins.get() != '/' );
ins >> frac.denominator;
return ins; // return the istream object
}
Nov 13, 2014 at 2:15am UTC
I've got it, thanks.
Topic archived. No new replies allowed.