P
P
Pinkman2020-09-24 01:24:35
C++ / C#
Pinkman, 2020-09-24 01:24:35

How to properly overload the input operator?

Good evening!
The code is like this:

istream&	operator>>(istream &stream, Rational &obj)
{
  int f;
  int l;
  stream >> f;
  stream.ignore(1);
  stream >> l;
  obj = {f, l};
  return stream;
}

int    main()
{
    istringstream input("5/7 10/8");
    Rational r1, r2;
    input >> r1 >> r2;
    bool correct = r1 == Rational(5, 7) && r2 == Rational(5, 4);
    if (!correct) {
      cout << "Multiple values are read incorrectly: " << r1 << " " << r2 << endl;
      return 4;
    }
}


In general, on the test, it crashes into an error. How can I fix it so that more than one object can be entered at a time?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question