c++ - no operator >> matches these operands -
i've been trying overload >> operator. have class has 2 private variables:
class complex { private: double real; double imaginary; }; in add-on have friend function overloads >> operator:
friend istream & operator>>(istream &is, complex &c) in implementation of function i've tried many ways write variable of object c maintain getting error no operator >> matches these operands
i looked around , read need write reference of variable, tried following:
istream & operator>>(istream &is, complex &c) { using std::cout; double &r = c.real; cout << "real: " << >> r; homecoming is; } however still gives me same error. i'm confused tried is >> c.real , didn't work.
on 1 of answers in similar question, suggested writing local variable , setting object variable it, like:
double d; cin >> d; setreal(d); i'm trying find more simpler way accomplish rather using method or setting variable local one.
the solution simple one, i'm beginner in c++, please take easy on me :p.
test case:
using std::cin; complex c; cin >> c; exact error message:
error 1 error c2678: binary '>>' : no operator found takes left-hand operand of type 'std::basic_ostream<_elem,_traits>' (or there no acceptable conversion)
the error on line:
cout << "real: " << >> r; this interpreted as
((cout << "real: ") << is) >> r the problem here can't have line switch outputting cout , start reading is. improve way be
cout << "real: "; >> r; that said, very bad idea. should not have operator >> display prompt, since means if want read in object of type file, every time prompt "real" displayed on-screen. should have operator >> read representation, , explicitly prompt before reading if that's want do.
hope helps!
c++ iostream istream
No comments:
Post a Comment