Reputation: 83
greeting all i am working on school project and i have a problem with my cod , what i am trying to do is returning two values. any help appreciated :)
#include <iostream>
#include <string>
using namespace std;
// class for rational number
class rational
{
public:
// input & output the fraction
void output(ostream& out);
void set(int num, int den);
int get_numerator();
int get_denominator();
// the operations
rational add(rational number);
rational sub(rational number);
rational mul(rational number);
rational div(rational number);
rational comp(rational number );
private:
int numerator;
int denominator;
};
rational rational1, rational2; // global veriables to use at comparing operations
rational rational::add(rational number) // addition operation
{
rational sum;
sum.numerator = numerator*number.denominator + denominator*number.numerator;
sum.denominator = denominator*number.denominator;
return sum ;
}
rational rational::sub(rational number) // subtract operation
{
rational sum2;
sum2.numerator =numerator*number.denominator - denominator*number.numerator;
sum2.denominator = denominator*number.denominator;
return sum2 ;
}
rational rational ::div(rational number) // divede operation
{
rational sum3 ;
sum3.numerator =numerator*number.denominator ;
sum3.denominator = denominator*number.numerator;
return sum3 ;
}
rational rational::mul(rational number) // multiply operation
{
rational sum4;
sum4.numerator =numerator*number.numerator;
sum4.denominator = denominator*number.denominator;
return sum4 ;
}
rational rational::comp (rational number ) // compare operation
{
rational compare1 , compare2 ;
compare1.denominator = numerator*number.denominator ;
compare2.numerator = denominator*number.numerator;
if (compare1.denominator > compare2.numerator)
{ cout << endl ;
cout << "the biggest fraction is ";
return rational1;
} else if (compare1.denominator < compare2.numerator )
{
cout << endl ;
cout << "the biggest fraction is ";
return rational2 ;
} else {
cout << " both fractions are equal ..." << rational2 << " = "; // here
is the error
return rational1 ;
}
}
void rational::set( int num, int den)
{
numerator = num;
denominator = den;
}
void rational::output(ostream& out = cout)
{
out << numerator << "/" << denominator;
}
int main ()
{
rational result ;
char salsh ;
cout << "please enter the numerator and denominator for the 1st fraction : ";
int num, den;
cin >> num >> salsh >> den;
if (den ==0)
{
cout << "the denominator can't be zero " << endl ;
system("pause");
return 0;
}
else
{
rational1.set(num, den);
cout << "please enter the numerator and denominator for the 2nd fraction :
" ;
cin >> num >>salsh >> den;
if (den ==0)
{
cout << "the denominator can't be zero " << endl ;
system("pause");
return 0;
}
else {
rational2.set(num, den);
result = rational1.add(rational2);
cout << "The result of adding them is: ";
result.output(cout);
cout << endl ;
result = rational1.sub(rational2);
cout << "The result of subtracting them is: ";
result.output(cout);
cout << endl ;
result = rational1.div(rational2);
cout << "The result of diveding them is: ";
result.output(cout);
cout << endl ;
result = rational1.mul(rational2);
cout << "The result multiplying them is: ";
result.output(cout);
cout << endl ;
result = rational1.comp(rational2) ;
result.output(cout);
cout << endl;
}
}
system ("pause");
return 0;
}
Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'rational' (or there is no acceptable conversion) c:\users\mike\documents\visual studio 2008\projects\mik\mik\michael shokery.cpp 73
here is the code
inline ostream& operator<<(ostream& stream, rational& rational)
{
rational.output(stream);
return stream;
}
Upvotes: 1
Views: 8857
Reputation: 27180
You need to overload operator << for the rational class.
See here: Overloading the << Operator for Your Own Classes
Upvotes: 2
Reputation: 92211
The difference is that in one place you do
cout << " both fractions are equal ..." << rational2 << " = "; // error here
while in the other places you do
cout << "The result of adding them is: ";
result.output(cout);
To have the << operator work for rational
you need to define the operator for that type
inline ostream& operator<<(ostream& stream, const rational& value)
{
value.output(stream);
return stream;
}
And to have the output function callable with the const
parameter to operator<<
, the function has to be const
as well:
class rational
{
public:
// input & output the fraction
void output(ostream& out) const; // Add const here
...
void rational::output(ostream& out = cout) const // and here!
{
out << numerator << "/" << denominator;
}
Upvotes: 1
Reputation: 2389
std::ostream has no << operator for your rational class, so it has no idea what to do with it. Define this:
ostream& operator << (ostream& stream, const rational& rat)
{
rat.output(stream);
return stream;
}
Upvotes: 3