主题:[讨论]关于模板的一个问题
报错:cannot convert 'this' pointer from 'const class Rational<int>' to 'class Rational<int> &'
源码:
template<typename T>
class Rational
{
public:
Rational(const T& numberator = 0,const T& denominator = 1):
m_numberator(numberator),m_denominator(denominator){}
~Rational(){}
const T numerator(){return m_numberator;}
const T denominator(){return m_denominator;}
friend const Rational operator*(const Rational& lhs,const Rational& rhs)
{
return Rational(lhs.numerator()*rhs.numerator(),lhs.denominator()*rhs.denominator());
}
private:
T m_numberator;
T m_denominator;
};
void main()
{
Rational<int> oneHalf(1,2);
Rational<int> result = oneHalf * 2;
}
源码:
template<typename T>
class Rational
{
public:
Rational(const T& numberator = 0,const T& denominator = 1):
m_numberator(numberator),m_denominator(denominator){}
~Rational(){}
const T numerator(){return m_numberator;}
const T denominator(){return m_denominator;}
friend const Rational operator*(const Rational& lhs,const Rational& rhs)
{
return Rational(lhs.numerator()*rhs.numerator(),lhs.denominator()*rhs.denominator());
}
private:
T m_numberator;
T m_denominator;
};
void main()
{
Rational<int> oneHalf(1,2);
Rational<int> result = oneHalf * 2;
}