我是一名c++编程课程的学生。我们被要求编写一个类模板来对不同数据类型的分数执行操作。
我设法重载了操作符,但我也想重载流插入操作符,以便在主程序中使用cout <<
。我相信答案是显而易见的任何人,除了我自己。代码是丑陋的,但我已经试着把方子钉进圆洞里两天了,没有让自己从头开始。
下面是带有类模板和成员定义的头文件。我最初在类定义中有函数friend
声明,在函数定义之外有函数定义的其余部分。这段代码不会产生任何编译错误,但是如果我试图在主程序中使用cout <<
,我得到这个错误:
Error C2275 'fractionType<int>': illegal use of this type as an expression McElroy_CS285_Week4Assignment C:UsersdavidsourcereposMcElroy_CS285_Week4AssignmentMcElroy_CS285_Week4AssignmentfractionType.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;
}
正如@273K所写,您应该使用变量名(fraction
),而不是类型名(fractionType
)。此外,您在operator<<
中使用非const引用。建议取const ref:
friend ostream& operator<<(ostream& osObject, const fractionType<Type>& fraction)
{
osObject << fraction.numerator << fraction.denominator;
return osObject;
}