0

this is the code

friend ostream& operator <<(ostream& output, const User& u)
        {
            output << endl << "User name :" << u.name << endl << "User id :" << u.id << endl << "User age :" << u.age << endl << "User email :" << u.email << endl << "User password :" << u.password << endl;
        }
        friend istream& operator >>(istream& input, User& u)
        {
            input >> u.name >> u.age >> u.email >> u.password;
        }

and this is the error visual studio gives me

77.cpp(94): error C4716: 'operator>>': must return a value 77.cpp(90): error C4716: 'operator<<': must return a value

Omar Walid
  • 25
  • 3
  • 1
    As the error states, you must return a value. See how your functions' signatures state that they return an `ostream&` and an `istream&`? Add `return output;` and `return input;` at the end of each function. – Nathan Pierson Nov 28 '20 at 18:58

1 Answers1

2

Your function signatures indicate that they're returning a value (as they should), but you're not returning anything.

Return the result of the expression that you're outputting with, so other code can use chain the use of operator<< or operator>>.

Ex:

friend ostream& operator <<(ostream& output, const User& u)
{
    return output << endl << "User name :" << u.name << endl << "User id :" << u.id << endl << "User age :" << u.age << endl << "User email :" << u.email << endl << "User password :" << u.password << endl;
}

Alternatively, you can simply add a return at the end:

friend ostream& operator <<(ostream& output, const User& u)
{
    output << endl << "User name :" << u.name << endl << "User id :" << u.id << endl << "User age :" << u.age << endl << "User email :" << u.email << endl << "User password :" << u.password << endl;
    return output;
}
Omry
  • 306
  • 1
  • 8