我知道這意味着什麼,但在我的情況下,我不明白爲什麼我的IDE爲此大喊大叫。表達式必須是我們班的一個可修改的左值
Rational operator*(const Rational& that1, const Rational& that2)
{
Rational temp(that1);
temp.getNom() *= that2.getNom();
temp.getDenom() *= that2.getDenom();
return temp;
}
int Rational::getNom() const
{
return m_iNom/gcd(m_iNom, m_iDenom);
}
int Rational::getDenom() const
{
return m_iDenom/gcd(m_iNom, m_iDenom);
}
float Rational::gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
m_iNom & m_iDenom是理性的類中私有數據成員。
我得到「表達必須修改的左值」在:
temp.getNom() *= that2.getNom();
temp.getDenom() *= that2.getDenom();
如果我想返回該怎麼辦nom和denom並通過函數調用來改變它,就像我在這裏做的那樣,那麼我是否需要做setter? – 2014-09-27 12:01:12
如果你被允許,調用setter('.setNom'?)或直接訪問該字段。 – 2014-09-27 12:02:59
希望我可以做到這一點,沒有setters,但好的感謝解釋 – 2014-09-27 12:05:21