Tutorial :operator inside operator not working



Question:

char b;    operator<<(cout,(operator>>(cin,b)));  

this is not compiling in vc++ because all 8 overloads cant convert this type.

can any one explain this.....

is their a problem with return type...........


Solution:1

The stream extraction operation i.e. op>> returns an object of type istream&. The op<< does not have an overload which takes istream& as its second parameter. You need to split the two actions or define one such overload.


Solution:2

The problem is that the output operator that would work takes a void*, but that is a member. If you change it to the following, it will convert the istream& returned by the operator>> to void* and output it (and it is a null pointer if the extraction worked, and a non-NULL pointer otherwise):

cout.operator<<(operator>>(cin,b));  

I'm not quite sure though why you are doing this. Can you please elaborate? If you want to output all stuff from cin right away, use the underlying buffer

cout << cin.rdbuf();  


Solution:3

char b;  operator<<(cout,(operator>>(cin,b),b));  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »