Question: I need help doing this fragment of code in C++. The step asks to write std::istream& operator >>(std::istream& is, rgb& colour) by: Defining the operator

I need help doing this fragment of code in C++. The step asks to write std::istream& operator >>(std::istream& is, rgb& colour) by:

Defining the operator overload of >>, i.e., std::istream& operator >>(std::istream& is, rgb& colour), as follows:

  • Read in an rgb structure into the colour parameter (i.e., the second parameter to this function).
    • The values are read in as whitespace-delimited numbers between 0 and 255.
    • The components will always appear in this order in the input stream: red, green, blue.
  • Regardless of how this function executes, its return statement is always return is; (i.e., the first std::istream& argument is returned).
  • If any component cannot be read in, then set the stream to "fail" with is.setstate(ios_base::failbit); and return from the function.
  • If any component cannot be read in, then ensure you never alter/update any values in colour (i.e., the second parameter to this function).
    • ADVICE: When reading in the components, declare them as local unsigned (not unsigned char) variables.
Code in C++: #include // for std::sqrt #include // for std::array #include // for std::vector #include // for std::numeric_limits #include // for std::string #include // for std::istream #include // for std::ostream #include // for std::cin, std::cout #include // for std::transform using namespace std; // place this after the #includes

struct rgb { unsigned char red,

unsigned char green;

unsigned char blue; };

//code goes here

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!