I am a student in a C programming course. We were asked to write a class template to perform operations on fractions of different data types.
I managed to overload the operators, but I wanted to overload the stream insertion operator also, to use cout <<
in the main program. I'm sure that the answer is obvious to anyone but myself. The code is ugly, but I have been trying to hammer square pegs into round holes for two days and haven't brought myself to start from scratch again.
Below is the header file with the class template and member definitions. I originally had the function friend
declaration in the class definition, and the function definition outside with the rest of the function definitions. This code doesn't generate any compilation errors, but if I try to use cout <<
in the main program, I get this error:
Error C2275 'fractionType<int>': illegal use of this type as an expression McElroy_CS285_Week4Assignment C:\Users\david\source\repos\McElroy_CS285_Week4Assignment\McElroy_CS285_Week4Assignment\fractionType.h 39
#pragma once
#include <iostream>
#include<math.h>
#include<iomanip>
using namespace std;
template <class Type>
class fractionType
{
public:
fractionType()
: numerator(0), denominator(0)
{
}
fractionType(Type num, Type den)
{
numerator = num;
denominator = den;
}
fractionType<Type> operator (const fractionType<Type>&) const;
fractionType<Type> operator-(const fractionType<Type>&) const;
fractionType<Type> operator*(const fractionType<Type>&) const;
fractionType<Type> operator/(const fractionType<Type>&) const;
bool operator==(const fractionType<Type>&) const;
bool operator!=(const fractionType<Type>&) const;
bool operator>(const fractionType<Type>&) const;
bool operator<(const fractionType<Type>&) const;
bool operator>=(const fractionType<Type>&) const;
bool operator<=(const fractionType<Type>&) const;
void print(fractionType<Type>);
friend ostream& operator<<(ostream& osObject, fractionType<Type>& fraction)
{
osObject << fractionType.numerator << fractionType.denominator;
return osObject;
}
private:
Type numerator;
Type denominator;
};
template<class Type>
fractionType<Type> fractionType<Type>::operator (const fractionType<Type>& fraction) const
{
fractionType<Type> tempFraction;
tempFraction.numerator = (numerator * fraction.denominator) (fraction.numerator * denominator);
tempFraction.denominator = fraction.denominator * denominator;
return tempFraction;
}
template<class Type>
fractionType<Type> fractionType<Type>::operator-(const fractionType& fraction) const
{
fractionType tempFraction;
tempFraction.numerator = (numerator * fraction.denominator) - (fraction.numerator * denominator);
tempFraction.denominator = fraction.denominator * denominator;
return tempFraction;
}
template<class Type>
fractionType<Type> fractionType<Type>::operator*(const fractionType& fraction) const
{
fractionType tempFraction;
tempFraction.numerator = numerator * fraction.numerator;
tempFraction.denominator = denominator * fraction.denominator;
return tempFraction;
}
template<class Type>
fractionType<Type> fractionType<Type>::operator/(const fractionType& fraction) const
{
fractionType tempFraction;
tempFraction.numerator = numerator * fraction.denominator;
tempFraction.denominator = fraction.numerator * denominator;
return tempFraction;
}
template<class Type>
bool fractionType<Type>::operator==(const fractionType& fraction) const
{
return (numerator / denominator == fraction.numerator / fraction.denominator);
}
template<class Type>
bool fractionType<Type>::operator!=(const fractionType& fraction) const
{
return(numerator / denominator != fraction.numerator / fraction.denominator);
}
template<class Type>
bool fractionType<Type>::operator>(const fractionType& fraction) const
{
return(numerator / denominator > fraction.numerator / fraction.denominator);
}
template<class Type>
bool fractionType<Type>::operator<(const fractionType& fraction) const
{
return(numerator / denominator < fraction.numerator / fraction.denominator);
}
template<class Type>
bool fractionType<Type>::operator>=(const fractionType& fraction) const
{
return(numerator / denominator >= fraction.numerator / fraction.denominator);
}
template<class Type>
bool fractionType<Type>::operator<=(const fractionType& fraction) const
{
return(numerator / denominator <= fraction.numerator / fraction.denominator);
}
template<class Type>
void fractionType<Type>::print(fractionType<Type> fraction)
{
cout << fraction.numerator << "/" << fraction.denominator << endl;
}
CodePudding user response:
As @273K wrote, you should use the variable name (fraction
), not the type name (fractionType
). Additionally, you take non-const reference in your operator<<
. Suggest taking const ref:
friend ostream& operator<<(ostream& osObject, const fractionType<Type>& fraction)
{
osObject << fraction.numerator << fraction.denominator;
return osObject;
}