Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

im writing in c++ and using codeblocks and am getting this error 1\\test_rationa

ID: 3863160 • Letter: I

Question

im writing in c++ and using codeblocks and am getting this error

1 est_rational.cpp|111|error: cannot bind 'std::istream {aka std::basic_istream<char>}' lvalue to 'std::basic_istream<char>&&'|

the code in question std::cin >> r1 >> op >> r2; It should be noted i didnt write the code, it was given by instructor.

void rc()
{
// Determine if input is coming from a terminal.
bool term = isatty(0);

// This will continue reading until it reaches the end-of-input.
// If you are using this interactively, type crtl-d to send the
// end of input character to the terminal.
while (std::cin)
{
Rational r1;
Rational r2;
std::string op;

if (term)
std::cout << "> ";

std::cin >> r1 >> op >> r2;
if (!std::cin)
break;

The istream operator is overwritten and this is the code for that

std::istream&
operator>>(std::istream& is, Rational& r)
{
if (!is)
return is;
int p, q;
char c;
is >> p;

c = is.peek(); // peek character
if ( c == '/' )
{
std::cin >> c >> q;
if (q == 0) {
is.setstate(std::ios::failbit);
return is;
}
}
else
{
c = '/';
q = 1;
}
std::cout << p <<c <<q << std::endl;
r = Rational(p, q);
return is;
}

Explanation / Answer

This error is coming because ...

This error of unable to bind.... comes when you are trying to use << or >> operators with user defined types but you have not given the implementation of how to handle this operator.

For example, in this code, there is a rational class, in the statement you are getting error contains:

std::cin >> r1 >> op >> r2;

Here, r1 and r2 are the objects of the Rational class. op is the string variable which is in-built data type so that you can directly use it with >> operator to get user input.

So, to make it work, you need to overload the >> operator inside the Rational class so that it handles input of an object with >>.

So either you have not still copied that code or you need to write that code as per the Rational class definition. This is the way to solve this error.

Please comment if there is any query. Thank you. :)