1

I am trying to use the istream function below to access the private data members numerator and denominator, however, I am getting errors about it being private. istream is a non-friend, non-member function (I cannot make it a friend). I understand that I cannot directly access the private members, but shouldn't my reference to MyFraction allow me to?

std::istream & operator>>(std::istream & sin, MyFraction & frac)

    {
        return sin >> frac.numerator >> frac.denominator;
    }

I have getters (getNumerator() and getDenominator()), however, I cannot access the members that way either. I also get an error:

ambiguous overload for operator>>.

My getters do return by value:

inline int MyFraction::getNumerator() const
{
    return numerator;
}

inline unsigned int MyFraction::getDenominator() const
{
    return denominator;
}

What am I overlooking in this function?

cc3072102
  • 13
  • 4
  • What does `MyFraction` look like? I suspect your getters return by value rather than by reference. If they returned by reference, then `sin >> frac.getNumerator() >> frac.getDenominator()` should work. Otherwise, you have to declare this function as a friend inside your `MyFraction`. – Nicu Stiurca Apr 07 '14 at 18:08
  • The "ambiguous overload" error makes me think you are trying to (re-)define a function that already exists... – twalberg Apr 07 '14 at 19:39

0 Answers0