Getting this error and I'm pretty sure it's in the operator<< function. Both prints are public.
void CRational::print() const
{
print(cout);
}
void CRational::print(ostream & sout) const
{
if(m_denominator == 1)
cout << m_numerator;
else
cout << m_numerator << "/" << m_denominator;
}
ostream operator<<(ostream & sout,const CRational a)
{
a.print();
return sout;
}
CRational operator++() // prefix ++x
{
m_numerator += m_denominator;
return *this;
}
in main:
cout << "e before: " << e << ", \"cout << ++e\" : " << ++e << " after: " << e << endl;
You need to return the ostream by reference, not value. Its trying to call a constructor. Might as well pass 'a' as reference as well:
ostream& operator<<(ostream & sout,const CRational& a)
I also note that the print method is probably wrong. It has sout
passed as the name of the stream but is then implemented using cout
directly. It should be
void CRational::print(ostream & sout) const
{
if(m_denominator == 1)
sout << m_numerator;
else
sout << m_numerator << "/" << m_denominator;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With