c++ - Error: cannot bind 'std::ostream {aka std::basic_ostream<char>} -
i searched through qs couldn't find answer.
i wanted overload operator<<
doesn't work me.
#include <iostream> #include <string> #include <tuple> class foo { public: std::tuple<int, float> tp; foo(int _a, float _b) { std::get<0>(tp)=_a; std::get<1>(tp) =_b; } friend std::ostream& operator<<(std::ostream & strm, const std::tuple<int, float> &tp) { strm << "[ "<<std::get<1>(tp)<<", "<<std::get<0>(tp)<<"]"<<"\n"; return strm; } }; int main () { foo a(1, 3.0f); std::cout<<a; return 0; }
error:
cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue 'std::basic_ostream<char>&&' std::cout<<a; ^
update solved, @juanchopanza
in order call std::cout<<a;
, need overload output stream operator has foo
second parameter. example:
friend std::ostream& operator<<(std::ostream& strm, const foo& foo) { return strm << "[ " << std::get<1>(foo.tp) << ", " << std::get<0>(foo.tp) << "]" << "\n"; }
Comments
Post a Comment